一、Java之ACM注意點
1. 類名稱必須采用public class Main方式命名
2. 在有些OJ系統上,即便是輸出的末尾多了一個“ ”,程序可能會輸出錯誤,所以在我看來好多OJ系統做的是非常之垃圾
3. 有些OJ上的題目會直接將OI上的題目拷貝過來,所以即便是題目中有輸入和輸出文件,可能也不需要,因為在OJ系統中一般是采用標准輸入輸出,不需要文件
4. 在有多行數據輸入的情況下,一般這樣處理,
- static Scanner in = new Scanner(System.in);
- while(in.hasNextInt())
- 或者是
- while(in.hasNext())
5. 有關System.nanoTime()函數的使用,該函數用來返回最准確的可用系統計時器的當前值,以毫微秒為單位。
- long startTime = System.nanoTime();
- long estimatedTime = System.nanoTime() - startTime;
二、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 67.7 90.5 12.8
- 5
- 56.9 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<n;i++){
- double a = sc.nextDouble();
- 。。。。。。
- }
- }
- }
- }
-
|
例3:讀入字符串【杭電2017 字符串統計】
輸入數據有多行,第一行是一個整數n,表示測試實例的個數,后面跟着n行,每行包括一個由字母和數字組成的字符串。
- Sample Input
- 2
- asdfasdf123123asdfasdf
- asdf111111111asdfasdfasdf
-
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- for(int i=0;i<n;i++){
- String str = sc.next();
- ......
- }
- }
- }
- 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;i<n;i++){
- String str = sc.nextLine();
- ......
- }
- }
- }
-
|
例3:讀入字符串【杭電2005 第幾天?】
- 給定一個日期,輸出這個日期是該年的第幾天。
- Input 輸入數據有多組,每組占一行,數據格式為YYYY/MM/DD組成
- 1985/1/20
- 2006/3/12
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};
- while(sc.hasNext()){
- int days = 0;
- String str = sc.nextLine();
- String[] date = str.split("/");
- int y = Integer.parseInt(date[0]);
- int m = Integer.parseInt(date[1]);
- int d = Integer.parseInt(date[2]);
- if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;
- days += d;
- for(int i=0;i<m;i++){
- days += dd[i];
- }
- System.out.println(days);
- }
- }
- }
|
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<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.
Output
For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.
Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2
Sample Output
3
-1
2
0.50
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc =new Scanner(System.in);
- int n = sc.nextInt();
- for(int i=0;i<n;i++){
- String op = sc.next();
- int a = sc.nextInt();
- int b = sc.nextInt();
- if(op.charAt(0)=='+'){
- System.out.println(a+b);
- }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) {
- NumberFormat formatter = new DecimalFormat( "000000");
- String s = formatter.format(-1234.567);
- System.out.println(s);
- formatter = new DecimalFormat( "##");
- s = formatter.format(-1234.567);
- System.out.println(s);
- s = formatter.format(0);
- System.out.println(s);
- formatter = new DecimalFormat( "##00");
- s = formatter.format(0);
- System.out.println(s);
-
- formatter = new DecimalFormat( ".00");
- s = formatter.format(-.567);
- System.out.println(s);
- formatter = new DecimalFormat( "0.00");
- s = formatter.format(-.567);
- System.out.println(s);
- formatter = new DecimalFormat( "#.#");
- s = formatter.format(-1234.567);
- System.out.println(s);
- formatter = new DecimalFormat( "#.######");
- s = formatter.format(-1234.567);
- System.out.println(s);
- formatter = new DecimalFormat( ".######");
- s = formatter.format(-1234.567);
- System.out.println(s);
- formatter = new DecimalFormat( "#.000000");
- s = formatter.format(-1234.567);
- System.out.println(s);
-
- formatter = new DecimalFormat( "#,###,###");
- s = formatter.format(-1234.567);
- System.out.println(s);
- s = formatter.format(-1234567.890);
- System.out.println(s);
-
-
- formatter = new DecimalFormat( "#;(#) ");
- s = formatter.format(-1234.567);
- System.out.println(s);
-
-
- formatter = new DecimalFormat( " '# '# ");
- s = formatter.format(-1234.567);
- System.out.println(s);
- formatter = new DecimalFormat( " 'abc '# ");
- s = formatter.format(-1234.567);
- System.out.println(s);
-
- formatter = new DecimalFormat( "#.##%");
- s = formatter.format(-12.5678987);
- System.out.println(s);
- }
|
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");
- }
- }
|
6. 進制轉換
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 {
- public static void main(String[] args) {
- 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] + " ");
- }
- }
|