tenth week

Posted 2017xinghui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tenth week相关的知识,希望对你有一定的参考价值。

1。限制负数答案出现,限制商为0

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.math.*;


public class Demo {
    public static void main(String[] args) throws FileNotFoundException {

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter("D://text.txt");;
        int sum = 0;     
        for (int i = 1; i <=10; i++) {
            int a = 1+(int)(Math.random() * 99);
            int b = 1+(int)(Math.random() * 99);
           int m= (int) Math.round(Math.random() * 3);
           switch(m)
           {
           case 0:
               while(a<b){  b = (int) Math.round(Math.random() * 100);a = (int) Math.round(Math.random() * 100); }  
               System.out.println(i+": "+a+"/"+b+"=");   
               int c0 = in.nextInt();
               out.println(a+"/"+b+"="+c0);
               if (c0 ==Math.floorDiv(a, b)) sum += 10;
               break; 
           case 1:
               System.out.println(i+": "+a+"*"+b+"=");
               int c = in.nextInt();
               out.println(a+"*"+b+"="+c);
               if (c ==Math.multiplyExact(a, b)) sum += 10;
               break;
           case 2:
               System.out.println(i+": "+a+"+"+b+"=");
               int c1 = in.nextInt();
               out.println(a+"+"+b+"="+c1);
               if (c1 == Math.addExact(a, b)) sum += 10;       
               break ;
           case 3:
               while(a<=b){  b = (int) Math.round(Math.random() * 100);a = (int) Math.round(Math.random() * 100); }    
               System.out.println(i+": "+a+"-"+b+"=");
               int c2 = in.nextInt();
               out.println(a+"-"+b+"="+c2);
               if (c2 ==Math.subtractExact(a, b)) sum += 10;
               break ;
               } 
          }     
        System.out.println("成绩"+sum);
        out.println("成绩:"+sum);
         out.close();      
    }
}

 技术分享图片技术分享图片

2。对除法实现实数的运算

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.math.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter("D://text2.txt");;
        int sum = 0;     
        for (int i = 1; i <=10; i++) {
            int a = 1+(int)(Math.random() * 99);
            int b = 1+(int)(Math.random() * 99);
            double a1 =a;  
            double b1 =b;
           int m= (int) Math.round(Math.random() * 3);
           switch(m)
           {
           case 0:
               while(a<b){  b = 1+(int)(Math.random() * 100);a = 1+(int)(Math.random() * 100); a1 =a;   b1=b;}  
               double c= Math.round((a1/b1)*100);
                 
               System.out.println(i+": "+a+"/"+b+"=");System.out.println("答案保留两位小数");   //System.out.println(i+": "+a+"/"+b+"="+c/100);  
               double c0 = in.nextDouble();
               out.println(a+"/"+b+"="+c0);
               if (c0 ==c/100) sum += 10;
               break; 
        case 1:
               System.out.println(i+": "+a+"*"+b+"=");
               int cc = in.nextInt();
               out.println(a+"*"+b+"="+cc);
               if (cc ==Math.multiplyExact(a, b)) sum += 10;
               break;
           case 2:
               System.out.println(i+": "+a+"+"+b+"=");
               int c1 = in.nextInt();
               out.println(a+"+"+b+"="+c1);
               if (c1 == Math.addExact(a, b)) sum += 10;       
               break ;
           case 3:
               while(a<=b){  b = (int) Math.round(Math.random() * 100);a = (int) Math.round(Math.random() * 100); }    
               System.out.println(i+": "+a+"-"+b+"=");
               int c2 = in.nextInt();
               out.println(a+"-"+b+"="+c2);
               if (c2 ==Math.subtractExact(a, b)) sum += 10;
               break ;     
               } 
          }     
        System.out.println("成绩"+sum);
        out.println("成绩:"+sum);
         out.close();      
    }
}

技术分享图片技术分享图片

test1

技术分享图片
package pair1;

/**
 * @version 1.01 2012-01-26
 * @author Cay Horstmann
 */
public class PairTest1
{
   public static void main(String[] args)
   {
      String[] words = { "Mary", "had", "a", "little", "lamb" };
      Pair<String> mm = ArrayAlg.minmax(words);
      System.out.println("min = " + mm.getFirst());
      System.out.println("max = " + mm.getSecond());
   }
}

class ArrayAlg
{
   /**
    * Gets the minimum and maximum of an array of strings.
    * @param a an array of strings
    * @return a pair with the min and max value, or null if a is null or empty
    */
   public static Pair<String> minmax(String[] a)
   {
      if (a == null || a.length == 0) return null;
      String min = a[0];
      String max = a[0];
      for (int i = 1; i < a.length; i++)
      {
         if (min.compareTo(a[i]) > 0) min = a[i];
         if (max.compareTo(a[i]) < 0) max = a[i];
      }
      return new Pair<>(min, max);
   }
}
PairTest1
技术分享图片
package pair1;

/**
 * @version 1.00 2004-05-10
 * @author Cay Horstmann
 */
public class Pair<T> 
{
   private T first;
   private T second;

   public Pair() { first = null; second = null; }
   public Pair(T first, T second) { this.first = first;  this.second = second; }

   public T getFirst() { return first; }
   public T getSecond() { return second; }

   public void setFirst(T newValue) { first = newValue; }
   public void setSecond(T newValue) { second = newValue; }
}
pair

技术分享图片

test2

技术分享图片
package pair2;

import java.time.*;

/**
 * @version 1.02 2015-06-21
 * @author Cay Horstmann
 */
public class PairTest2
{
   public static void main(String[] args)
   {
      LocalDate[] birthdays = 
         { 
            LocalDate.of(1906, 12, 9), // G. Hopper
            LocalDate.of(1815, 12, 10), // A. Lovelace
            LocalDate.of(1903, 12, 3), // J. von Neumann
            LocalDate.of(1910, 6, 22), // K. Zuse
         };
      Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);
      System.out.println("min = " + mm.getFirst());
      System.out.println("max = " + mm.getSecond());
   }
}

class ArrayAlg
{
   /**
      Gets the minimum and maximum of an array of objects of type T.
      @param a an array of objects of type T
      @return a pair with the min and max value, or null if a is 
      null or empty
   */
   public static <T extends Comparable> Pair<T> minmax(T[] a) 
   {
      if (a == null || a.length == 0) return null;
      T min = a[0];
      T max = a[0];
      for (int i = 1; i < a.length; i++)
      {
         if (min.compareTo(a[i]) > 0) min = a[i];
         if (max.compareTo(a[i]) < 0) max = a[i];
      }
      return new Pair<>(min, max);
   }
}
PairTest2
技术分享图片
package pair2;

/**
 * @version 1.00 2004-05-10
 * @author Cay Horstmann
 */
public class Pair<T> 
{
   private T first;
   private T second;

   public Pair() { first = null; second = null; }
   public Pair(T first, T second) { this.first = first;  this.second = second; }

   public T getFirst() { return first; }
   public T getSecond() { return second; }

   public void setFirst(T newValue) { first = newValue; }
   public void setSecond(T newValue) { second = newValue; }
}
pair

技术分享图片

test3

技术分享图片
package pair3;

/**
 * @version 1.01 2012-01-26
 * @author Cay Horstmann
 */
public class PairTest3
{
   public static void main(String[] args)
   {
      Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
      Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
      Pair<Manager> buddies = new Pair<>(ceo, cfo);      
      printBuddies(buddies);

      ceo.setBonus(1000000);
      cfo.setBonus(500000);
      Manager[] managers = { ceo, cfo };

      Pair<Employee> result = new Pair<>();
      minmaxBonus(managers, result);
      System.out.println("first: " + result.getFirst().getName() 
         + ", second: " + result.getSecond().getName());
      maxminBonus(managers, result);
      System.out.println("first: " + result.getFirst().getName() 
         + ", second: " + result.getSecond().getName());
   }

   public static void printBuddies(Pair<? extends Employee> p)
   {
      Employee first = p.getFirst();
      Employee second = p.getSecond();
      System.out.println(first.getName() + " and " + second.getName() + " are buddies.");
   }

   public static void minmaxBonus(Manager[] a, Pair<? super Manager> result)
   {
      if (a.length == 0) return;
      Manager min = a[0];
      Manager max = a[0];
      for (int i = 1; i < a.length; i++)
      {
         if (min.getBonus() > a[i].getBonus()) min = a[i];
         if (max.getBonus() < a[i].getBonus()) max = a[i];
      }
      result.setFirst(min);
      result.setSecond(max);
   }

   public static void maxminBonus(Manager[] a, Pair<? super Manager> result)
   {
      minmaxBonus(a, result);
      PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type
   }
   // Can‘t write public static <T super manager> ...
}

class PairAlg
{
   public static boolean hasNulls(Pair<?> p)
   {
      return p.getFirst() == null || p.getSecond() == null;
   }

   public static void swap(Pair<?> p) { swapHelper(p); }

   public static <T> void swapHelper(Pair<T> p)
   {
      T t = p.getFirst();
      p.setFirst(p.getSecond());
      p.setSecond(t);
   }
}
PairTest3
技术分享图片
package pair3;

import java.time.*;

public class Employee
{  
   private String name;
   private double salary;
   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)
   {
      this.name = name;
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {  
      return salary;
   }

   public LocalDate getHireDay()
   {  
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {  
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}
Employee
技术分享图片
package pair3;

public class Manager extends Employee
{  
   private double bonus;

   /**
      @param name the employee‘s name
      @param salary the salary
      @param year the hire year
      @param month the hire month
      @param day the hire day
   */
   public Manager(String name, double salary, int year, int month, int day)
   {  
      super(name, salary, year, month, day);
      bonus = 0;
   }

   public double getSalary()
   { 
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
   }

   public void setBonus(double b)
   {  
      bonus = b;
   }

   public double getBonus()
   {  
      return bonus;
   }
}
Manager extends Employee
技术分享图片
package pair3;

/**
 * @version 1.00 2004-05-10
 * @author Cay Horstmann
 */
public class Pair<T> 
{
   private T first;
   private T second;

   public Pair() { first = null; second = null; }
   public Pair(T first, T second) { this.first = first;  this.second = second; }

   public T getFirst() { return first; }
   public T getSecond() { return second; }

   public void setFirst(T newValue) { first = newValue; }
   public void setSecond(T newValue) { second = newValue; }
}
pair

技术分享图片

 

 

---

程序要感谢学长的第一次读文件的模板,有了大体框架就只剩下核心算法,

修改程序后就能达到要求,通过学习课本知识和网上资料,对要实现的操作要求,

合理运用已经学习的知识和算法的例子就可以实现,虽然报错调试优化的时间占大多数,

但好歹时间充裕,努力努力就完成了。

----

第一个实验问题

读取每行内容时按空格" "为分界符读取到住址时不能合理读取,最后换成读取每行剩余内容完成要求。

查询最大最小年龄时读取年龄数据是String,无法比较大小,最后将数据强制换成int进行比较。

实现找同乡操作时,由于之前实现过相同的算法,完成起来较为轻松

实现查找同龄人操作时,先是只输出一个查找到的人,但看txt文本中有相同年龄的人

应该一起输出,最后完成的仍然有问题,但做的时间太长了很烦躁,之后没有优化

其他琐碎的问题不想说,大多是新知识不能合理运用和语法格式的问题

第二个实验

上周做关于整数的时候问题是如何随机生成四则运算,最后用决定switch语句实现

这一周实现实数的运算问题是用math类中方法保存两位小数

以上是关于tenth week的主要内容,如果未能解决你的问题,请参考以下文章

Oldboy tenth day . I love Python. Tenth day- second 初识函数def()

[ LeetCode试题 ] 195 Tenth Line

ROS2机器人f1tenth之CLI工具基础

[LeetCode] Tenth Line

195 Tenth Line

蓝桥ROS之f1tenth案例学习与调试(成功)