回归练习行列,完成2262 1503
poj2262在调整了一下让i=i+3之后运行时间减少了一点,但还没有完全符合要求,看来我做得还算不够好,需要更加简洁的方法才行。
1 import java.io.BufferedInputStream; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String args[]) { 7 8 Scanner cin = new Scanner(new BufferedInputStream(System.in)); 9 int num; 10 boolean flag = false; 11 12 while (hasNext(num = cin.nextInt())) { 13 14 flag = false; 15 16 if (num >= 6 && num <= 1000000 && num % 2 == 0) { 17 for (int i = 3; i + i <= num; i=i+2) { 18 if (Isprime(i) && Isprime(num - i)) { 19 System.out.println(num + " = " + i + " + " + (num - i)); 20 flag = true; 21 break; 22 } 23 } 24 25 if (!flag) { 26 System.out.println("Goldbach's conjecture is wrong."); 27 } 28 } else { 29 System.out.println("The input is wrong!"); 30 } 31 32 } 33 34 } 35 36 public static boolean hasNext(int num) { 37 if (num == 0) { 38 return false; 39 } 40 return true; 41 } 42 43 public static boolean Isprime(int num) { 44 45 for (int i = 3; i <= Math.sqrt(num); i=i+2) { 46 47 if (num % i == 0) 48 return false; 49 50 } 51 52 return true; 53 54 } 55 56 }
poj1503则是令人尴尬不懂,看来基本功不足啊,主要使用BigDecimal,然后使一个总的重复加上读取的下一行。
1 import java.io.BufferedInputStream; 2 import java.math.BigDecimal; 3 import java.util.Scanner; 4 5 public class Main { 6 7 public static void main(String args[]) { 8 9 Scanner cin = new Scanner(new BufferedInputStream(System.in)); 10 11 BigDecimal bd1 = BigDecimal.valueOf(0); 12 BigDecimal bd2 = BigDecimal.valueOf(0); 13 String str; 14 15 while (cin.hasNext()) { 16 17 str = cin.nextLine(); 18 if (str.equals("0")) { 19 break; 20 } 21 22 bd2 = new BigDecimal(str); 23 bd1 = bd1.add(bd2); 24 25 26 27 } 28 29 System.out.println(bd1.toPlainString()); 30 31 } 32 33 }
发了两天烧,今天才去打了针,以后真的要注意了。最重要的是,要好好锻炼身体了,没有好身体,行动都困难,何谈未来,何谈梦想啊,