logo资料库

java进行ACM读入输出.doc

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
ACM 之 java 输入输出 一、Java 基本开发环境 1. JDK 1.5 以上 编译运行环境 2. Ecllipse 3.2 以上开发环境 二、使用 Java 做题之优缺点 1. 优点 2. 缺点 三、Java 之输入输出处理 由于 ACM 竞赛题目的输入数据和输出数据一般有多组(不定),并 且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最 基本的要求。这也是困扰初学者的一大问题。 1. 输入: 格式 1:Scanner sc = new Scanner (new BufferedInputStream(System.in)); 格式 2:Scanner sc = new Scanner (System.in); 在读入数据量大的情况下,格式 1 的速度会快些。 读一个整数: int n = sc.nextInt(); 相当于 scanf("%d", &n); 或 cin >> n; 读一个字符串:String s = sc.next(); 相当于 scanf("%s", s); 或 cin >> s; 读一个浮点数:double t = sc.nextDouble(); 相当于 scanf("%lf", &t); 或 cin >> t; 读一整行: String s = sc.nextLine(); 相当于 gets(s); 或 cin.getline(...); 判 断 是 否 有 下 一 个 输 入 可 以 用 sc.hasNext() 或 sc.hasNextInt() 或 sc.hasNextDouble() 或 sc.hasNextLine() 例 1:读入整数 Input 输入数据有多组,每组占一行,由一个整数组成。 Sample Input 56 67 100 123 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in);
while(sc.hasNext()){ //判断是否结束 int score = sc.nextInt(); 。。。。 //读入整数 } } } 例 2:读入实数 输入数据有多组,每组占 2 行,第一行为一个整数 N,指示第二行包含 N 个实数。 Sample Input 4 56.9 5 56.9 90.5 67.7 12.8 67.7 90.5 12.8 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); for(int i=0;i
} import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); for(int i=0;i2) days ++; days += d; for(int i=0;i
 http://acm.hdu.edu.cn/showproblem.php?pid=1092  http://acm.hdu.edu.cn/showproblem.php?pid=1093  http://acm.hdu.edu.cn/showproblem.php?pid=1094 2. 输出 函数: System.out.print(); System.out.println(); System.out.format(); System.out.printf(); 例4 杭电1170Balloon Comes! Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result. Input Input contains multiple test cases. The first line of the input is a single integer T (0
}else if(op.charAt(0)=='-'){ System.out.println(a-b); }else if(op.charAt(0)=='*'){ System.out.println(a*b); }else if(op.charAt(0)=='/'){ if(a % b == 0) System.out.println(a / b); else System.out.format("%.2f", (a / (1.0*b))). Println(); } } } } 3. 规格化的输出: 函数: // 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入. DecimalFormat fd = new DecimalFormat("#.00#"); DecimalFormat gd = new DecimalFormat("0.000"); System.out.println("x =" + fd.format(x)); System.out.println("x =" + gd.format(x)); public static void main(String[] args) { formatter formatter.format(-1234.567); = new DecimalFormat( "000000"); // -001235 = = new NumberFormat String s = System.out.println(s); formatter s System.out.println(s); s System.out.println(s); formatter s System.out.println(s); new = = = new = = = = new formatter s System.out.println(s); formatter s System.out.println(s); formatter s System.out.println(s); formatter s new new = = = = DecimalFormat( "##"); formatter.format(-1234.567); formatter.format(0); DecimalFormat( "##00"); formatter.format(0); // -1235 // 0 // 00 // -.57 DecimalFormat( ".00"); formatter.format(-.567); formatter.format(-.567); DecimalFormat( "0.00"); // -0.57 DecimalFormat( "#.#"); formatter.format(-1234.567); // -1234.6 DecimalFormat( "#.######"); formatter.format(-1234.567); // -1234.567
= = new System.out.println(s); formatter s System.out.println(s); formatter s System.out.println(s); new = = new = = formatter s System.out.println(s); s System.out.println(s); = DecimalFormat( ".######"); formatter.format(-1234.567); // -1234.567 DecimalFormat( "#.000000"); formatter.format(-1234.567); // -1234.567000 DecimalFormat( "#,###,###"); formatter.format(-1234.567); // -1,235 formatter.format(-1234567.890); // -1,234,568 // The ; symbol used to specify an alternate pattern for negative is values new = formatter s System.out.println(s); = DecimalFormat( "#;(#) "); formatter.format(-1234.567); // (1235) is used to quote literal symbols DecimalFormat( " '# '# "); formatter.format(-1234.567); // -#1235 DecimalFormat( " 'abc '# "); formatter.format(-1234.567); // - abc 1235 = = ' The symbol new // formatter s System.out.println(s); formatter s System.out.println(s); new = = new = formatter s System.out.println(s); = formatter.format(-12.5678987); DecimalFormat( "#.##%"); }  http://acm.hdu.edu.cn/showproblem.php?pid=1012  4. 字符串处理 String String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始: String a = "Hello"; // a.charAt(1) = 'e' 用substring方法可得到子串,如上例 System.out.println(a.substring(0, 4)) // output "Hell" 注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。 字符串连接可以直接用 + 号,如 String a = "Hello"; String b = "world"; System.out.println(a + ", " + b + "!"); // output "Hello, world!"
如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。 5. 高精度 BigInteger和BigDecimal可以说是acmer选择java的首要原因。 函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是 BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换 为BigInteger(BigDecimal),用函数BigInteger.valueOf(). 例程: import java.io.BufferedInputStream; import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner (new BufferedInputStream(System.in)); int a = 123, b = 456, c = 7890; BigInteger x, y, z, ans; x = BigInteger.valueOf(a); y = BigInteger.valueOf(b); z = BigInteger.valueOf(c); ans = x.add(y); System.out.println(ans); ans = z.divide(y); System.out.println(ans); ans = x.mod(z); System.out.println(ans); if (ans.compareTo(x) == 0) System.out.println("1"); } }  http://acm.hdu.edu.cn/showproblem.php?pid=1018  http://acm.hdu.edu.cn/showproblem.php?pid=1013  http://acm.hdu.edu.cn/showproblem.php?pid=1002 6. 进制转换 java很强大的一个功能。 函数: String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35). int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数, 第一个为要转的字符串,第二个为说明是什么进制). BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制. 7. 数组排序 函数:Arrays.sort();
public class Main { Scanner cin = new Scanner (new BufferedInputStream(System.in)); int n = cin.nextInt(); int a[] = new int [n]; for (int i = 0; i < n; i++) a[i] = cin.nextInt(); Arrays.sort(a); for (int i = 0; i < n; i++) System.out.print(a[i] + " "); public static void main(String[] args) { } }
分享到:
收藏