注:题目请查看杭电ACM网站
不知道杭电acm编译通过的标准是什么,所以本题贴多种形式的代码。
由于数值大小问题,不能使用int类型,所以用String类型,也不用long
编译通过的:
import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String []args) { Scanner sc = new Scanner(System.in); int col = sc.nextInt(); if (1 <= col && col <= 20) { int row = 2; String[][] jihe = new String[col][row]; String value1 = null; String value2 = null; for (int i = 0; i < col; i++) { value1 = sc.next(); value2 = sc.next(); if(i!=(col-1)) { System.out.println("Case"+" "+ (i+1)+":\r\n"+value1+" + "+value2+" = "+new BigDecimal(value1).add(new BigDecimal(value2))+"\r\n"); } else { System.out.println("Case"+" "+(i+1) +":\r\n"+value1+" + "+value2+" = "+new BigDecimal(value1).add(new BigDecimal(value2))); } } } } }
编译不通过,但是在IDE上运行没问题的:
形式1(这个形式和编译通过的区别在于输出的判断情况上。。。)
import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String []args) { Scanner sc = new Scanner(System.in); int col = sc.nextInt(); if (1 <= col && col <= 20) { int row = 2; String[][] jihe = new String[col][row]; String value1 = null; String value2 = null; for (int i = 0; i < col; i++) { value1 = sc.next(); value2 = sc.next(); //与编译通过的区别之处 System.out.println("\r\n" + "Case " + (i + 1) + ":"); System.out.println(value1 + " + " + value2 + " = " + new BigDecimal(value1).add(new BigDecimal(value2)) + "\r\n"); } } } }
形式2(这个形式考虑到输入错误时可重新输入)
import java.util.Scanner; import java.math.BigDecimal; public class Main2 { static boolean isTrue = false; public static void main(String []args){ Scanner sc = new Scanner(System.in); int col = sc.nextInt(); new Main2().saveValue(col); while(!isTrue){ col = sc.nextInt(); new Main2().saveValue(col); } } private void saveValue(int col){ if(1 <= col && col<=20){ Scanner sc = new Scanner(System.in); int row =2; String [][]jihe = new String[col][row]; String value1 = null; String value2 = null; for(int m=0;m<col;m++) { value1 = sc.next(); value2 = sc.next(); for(int n=0;n<row-1;n++){ jihe[m][n]=value1; jihe[m][++n]=value2; } } for(int i = 0; i<col; i++){ for(int j=0; j<row-1;j++){ if(i!=(col-1)) { System.out.println("Case"+" "+ (i+1)+":\r\n"+value1+" + "+value2+" = "+new BigDecimal(value1).add(new BigDecimal(value2))+"\r\n"); } else { System.out.println("Case"+" "+(i+1) +":\r\n"+value1+" + "+value2+" = "+new BigDecimal(value1).add(new BigDecimal(value2))); } // System.out.println("Case "+i + ":"); // System.out.println(jihe[i][j] + " + " + jihe[i][++j] + " = " + new BigDecimal(jihe[i][--j]).add(new BigDecimal(jihe[i][++j])) + "\n"); } } isTrue=true; }else{ System.out.println("输入的计算数目不符合大于等于1或者小于等于20!请重新输入!"); isTrue = false; } } }
本题疑惑点,在于编译通过的限制条件是什么?!
文章来源: 杭电acm 1002