【經驗總結】Java在ACM算法競賽編程中易錯點


一、Java之ACM易錯點

 

1. 類名稱必須采用public class Main方式命名

 

2. 在有些OJ系統上,即便是輸出的末尾多了一個“ ”,程序可能會輸出錯誤,所以在我看來好多OJ系統做的是非常之垃圾

 

3. 有些OJ上的題目會直接將OI上的題目拷貝過來,所以即便是題目中有輸入和輸出文件,可能也不需要,因為在OJ系統中一般是采用標准輸入輸出,不需要文件

 

4. 在有多行數據輸入的情況下,一般這樣處理:

1 static Scanner in = new Scanner(System.in);  
2 while(in.hasNextInt())  
3 或者是  
4 while(in.hasNext())  

 

5. 有關System.nanoTime()函數的使用,該函數用來返回最准確的可用系統計時器的當前值,以毫微秒為單位。

 

  1. long startTime = System.nanoTime();  
  2. // ... the code being measured ...  
  3. long estimatedTime = System.nanoTime() - startTime; 

 

 

二、Java之輸入輸出處理

 

由於ACM競賽題目的輸入數據和輸出數據一般有多組(不定),並且格式多種多樣,所以,如何處理題目的輸入輸出是對大家的一項最基本的要求。這也是困擾初學者的一大問題。

 

1. 輸入:

 

格式1Scanner sc = new Scanner (new BufferedInputStream(System.in));

 

格式2Scanner 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:讀入整數

 

  1. Input  輸入數據有多組,每組占一行,由一個整數組成。   
  2. Sample Input   
  3. 56  
  4. 67  
  5. 100  
  6. 123   
  7.    
  8. import java.util.Scanner;  
  9. public class Main {  
  10. public static void main(String[] args) {  
  11. Scanner sc =new Scanner(System.in);  
  12. while(sc.hasNext()){  //判斷是否結束  
  13. int score = sc.nextInt();//讀入整數  
  14. 。。。。  
  15. }  
  16. }  
  17. }  
  18.    



 

 

2:讀入實數

輸入數據有多組,每組占2行,第一行為一個整數N,指示第二行包含N個實數。

 

  1. Sample Input  
  2. 4   
  3. 56.9  67.7  90.5  12.8   
  4. 5   
  5. 56.9  67.7  90.5  12.8   
  6.    
  7. import java.util.Scanner;  
  8. public class Main {  
  9. public static void main(String[] args) {  
  10. Scanner sc =new Scanner(System.in);  
  11. while(sc.hasNext()){  
  12. int n = sc.nextInt();  
  13. for(int i=0;i<n;i++){  
  14. double a = sc.nextDouble();  
  15. 。。。。。。  
  16. }  
  17. }  
  18. }  
  19. }  
  20.    



 

3:讀入字符串【杭電2017 字符串統計

 

輸入數據有多行,第一行是一個整數n,表示測試實例的個數,后面跟着n行,每行包括一個由字母和數字組成的字符串。

 

  1. Sample Input    
  2. 2  
  3. asdfasdf123123asdfasdf  
  4. asdf111111111asdfasdfasdf  
  5.    
  6. import java.util.Scanner;  
  7. public class Main {  
  8. public static void main(String[] args) {  
  9. Scanner sc = new Scanner(System.in);  
  10. int n = sc.nextInt();  
  11. for(int i=0;i<n;i++){  
  12. String str = sc.next();  
  13. ......  
  14. }  
  15. }  
  16. }  
  17. import java.util.Scanner;  
  18. public class Main {  
  19. public static void main(String[] args) {  
  20. Scanner sc = new Scanner(System.in);  
  21. int n = Integer.parseInt(sc.nextLine());  
  22. for(int i=0;i<n;i++){  
  23. String str = sc.nextLine();  
  24. ......  
  25. }  
  26. }  
  27. }  
  28.    



 

 

3:讀入字符串【杭電2005 第幾天?

  1. 給定一個日期,輸出這個日期是該年的第幾天。   
  2. Input  輸入數據有多組,每組占一行,數據格式為YYYY/MM/DD組成  
  3. 1985/1/20  
  4. 2006/3/12  
  5. import java.util.Scanner;  
  6. public class Main {  
  7. public static void main(String[] args) {  
  8. Scanner sc = new Scanner(System.in);  
  9. int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};  
  10. while(sc.hasNext()){  
  11. int days = 0;  
  12. String str = sc.nextLine();  
  13. String[] date = str.split("/");  
  14. int y = Integer.parseInt(date[0]);  
  15. int m = Integer.parseInt(date[1]);  
  16. int d = Integer.parseInt(date[2]);  
  17. if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;  
  18. days += d;  
  19. for(int i=0;i<m;i++){  
  20. days += dd[i];  
  21. }  
  22. System.out.println(days);  
  23. }  
  24. }  
  25. }  



 

 

 

 

2. 輸出  

 

函數:

 

System.out.print(); 

 

System.out.println(); 

 

System.out.format();

 

System.out.printf();  

 

 

 

杭電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

  1. import java.util.Scanner;  
  2. public class Main {  
  3. public static void main(String[] args) {  
  4. Scanner sc =new Scanner(System.in);  
  5. int n = sc.nextInt();  
  6. for(int i=0;i<n;i++){  
  7. String op = sc.next();  
  8. int a = sc.nextInt();  
  9. int b = sc.nextInt();  
  10. if(op.charAt(0)=='+'){  
  11. System.out.println(a+b);  
  12. }else if(op.charAt(0)=='-'){  
  13. System.out.println(a-b);  
  14. }else if(op.charAt(0)=='*'){  
  15. System.out.println(a*b);  
  16. }else if(op.charAt(0)=='/'){  
  17. if(a % b == 0) System.out.println(a / b);  
  18. else System.out.format("%.2f", (a / (1.0*b))). Println();  
  19. }  
  20. }  
  21. }  
  22. }  

 


 

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));

  1. public static void main(String[] args) {  
  2.     NumberFormat   formatter   =   new   DecimalFormat( "000000");   
  3.         String  s  =   formatter.format(-1234.567);     //   -001235   
  4.         System.out.println(s);  
  5.         formatter   =   new   DecimalFormat( "##");   
  6.         s   =   formatter.format(-1234.567);             //   -1235   
  7.         System.out.println(s);  
  8.         s   =   formatter.format(0);                      //   0   
  9.         System.out.println(s);  
  10.         formatter   =   new   DecimalFormat( "##00");   
  11.         s   =   formatter.format(0);                     //   00   
  12.         System.out.println(s);  
  13.    
  14.         formatter   =   new   DecimalFormat( ".00");   
  15.         s   =   formatter.format(-.567);               //   -.57   
  16.         System.out.println(s);  
  17.         formatter   =   new   DecimalFormat( "0.00");   
  18.         s   =   formatter.format(-.567);              //   -0.57   
  19.         System.out.println(s);  
  20.         formatter   =   new   DecimalFormat( "#.#");   
  21.         s   =   formatter.format(-1234.567);         //   -1234.6   
  22.         System.out.println(s);  
  23.         formatter   =   new   DecimalFormat( "#.######");   
  24.         s   =   formatter.format(-1234.567);        //   -1234.567   
  25.         System.out.println(s);  
  26.         formatter   =   new   DecimalFormat( ".######");   
  27.         s   =   formatter.format(-1234.567);       //   -1234.567   
  28.         System.out.println(s);  
  29.         formatter   =   new   DecimalFormat( "#.000000");   
  30.         s   =   formatter.format(-1234.567);      //   -1234.567000   
  31.         System.out.println(s);  
  32.           
  33.         formatter   =   new   DecimalFormat( "#,###,###");   
  34.         s   =   formatter.format(-1234.567);      //   -1,235   
  35.         System.out.println(s);  
  36.         s   =   formatter.format(-1234567.890);  //   -1,234,568   
  37.         System.out.println(s);  
  38.    
  39.         //   The   ;   symbol   is   used   to   specify   an   alternate   pattern   for   negative   values   
  40.         formatter   =   new   DecimalFormat( "#;(#) ");   
  41.         s   =   formatter.format(-1234.567);     //   (1235)   
  42.         System.out.println(s);  
  43.    
  44.         //   The   '   symbol   is   used   to   quote   literal   symbols   
  45.         formatter   =   new   DecimalFormat( " '# '# ");   
  46.         s   =   formatter.format(-1234.567);        //   -#1235   
  47.         System.out.println(s);  
  48.         formatter   =   new   DecimalFormat( " 'abc '# ");   
  49.         s   =   formatter.format(-1234.567);      // - abc 1235  
  50.         System.out.println(s);  
  51.    
  52. formatter   =   new   DecimalFormat( "#.##%");   
  53.         s   =   formatter.format(-12.5678987);    
  54.         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類。 

 

  1. import java.io.BufferedInputStream;  
  2. import java.math.BigInteger;  
  3. import java.util.Scanner;  
  4. public class Main {  
  5. public static void main(String[] args)   {  
  6. Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  7.         int a = 123, b = 456, c = 7890;  
  8.         BigInteger x, y, z, ans;  
  9.         x = BigInteger.valueOf(a);   
  10.         y = BigInteger.valueOf(b);   
  11.         z = BigInteger.valueOf(c);  
  12.         ans = x.add(y); System.out.println(ans);  
  13.         ans = z.divide(y); System.out.println(ans);  
  14.         ans = x.mod(z); System.out.println(ans);  
  15.         if (ans.compareTo(x) == 0) System.out.println("1");  
  16.     }  
  17. }  




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是字符串,basest的進制.
7. 數組排序
函數:Arrays.sort();

 

5. 高精度
BigIntegerBigDecimal可以說是acmer選擇java的首要原因。
函數:add, subtract, divide, mod, compareTo等,其中加減乘除模都要求是BigInteger(BigDecimal)BigInteger(BigDecimal)之間的運算,所以需要把int(double)類型轉換為BigInteger(BigDecimal),用函數BigInteger.valueOf().

 

  1. public class Main {  
  2. public static void main(String[] args)    {  
  3.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  4.         int n = cin.nextInt();  
  5.         int a[] = new int [n];  
  6.         for (int i = 0; i < n; i++) a[i] = cin.nextInt();  
  7.         Arrays.sort(a);  
  8.         for (int i = 0; i < n; i++) System.out.print(a[i] + " ");  
  9.     }  
  10. }  


易錯:

1.for(int i=m;i<n;i++){isFlowerNum(m);}  //這里m是不變量,應該用i

2.m=m/10的值就變化了如果想要繼續用m,應該提前保存

 

 

 

 

 

 

 

一、Java之ACM注意點

1. 類名稱必須采用public class Main方式命名

2. 在有些OJ系統上,即便是輸出的末尾多了一個“ ”,程序可能會輸出錯誤,所以在我看來好多OJ系統做的是非常之垃圾

3. 有些OJ上的題目會直接將OI上的題目拷貝過來,所以即便是題目中有輸入和輸出文件,可能也不需要,因為在OJ系統中一般是采用標准輸入輸出,不需要文件

4. 在有多行數據輸入的情況下,一般這樣處理,

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. static Scanner in = new Scanner(System.in);  
  2. while(in.hasNextInt())  
  3. 或者是  
  4. while(in.hasNext())  

5. 有關System.nanoTime() 函數的使用,該函數用來 返回最准確的可用系統計時器的當前值,以毫微秒為單位。

 

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. long startTime = System.nanoTime();  
  2. // ... the code being measured ...  
  3. long estimatedTime = System.nanoTime() - startTime;  

 

二、Java之輸入輸出處理

由於ACM競賽題目的輸入數據和輸出數據一般有多組(不定),並且格式多種多樣,所以,如何處理題目的輸入輸出是對大家的一項最基本的要求。這也是困擾初學者的一大問題。

1. 輸入:

格式1Scanner sc = new Scanner (new BufferedInputStream(System.in));

格式2Scanner 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:讀入整數

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. Input  輸入數據有多組,每組占一行,由一個整數組成。   
  2. Sample Input   
  3. 56  
  4. 67  
  5. 100  
  6. 123   
  7.    
  8. import java.util.Scanner;  
  9. public class Main {  
  10. public static void main(String[] args) {  
  11. Scanner sc =new Scanner(System.in);  
  12. while(sc.hasNext()){  //判斷是否結束  
  13. int score = sc.nextInt();//讀入整數  
  14. 。。。。  
  15. }  
  16. }  
  17. }  
  18.    


 

2:讀入實數

 

輸入數據有多組,每組占2行,第一行為一個整數N,指示第二行包含N個實數。

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. Sample Input  
  2. 4   
  3. 56.9  67.7  90.5  12.8   
  4. 5   
  5. 56.9  67.7  90.5  12.8   
  6.    
  7. import java.util.Scanner;  
  8. public class Main {  
  9. public static void main(String[] args) {  
  10. Scanner sc =new Scanner(System.in);  
  11. while(sc.hasNext()){  
  12. int n = sc.nextInt();  
  13. for(int i=0;i<n;i++){  
  14. double a = sc.nextDouble();  
  15. 。。。。。。  
  16. }  
  17. }  
  18. }  
  19. }  
  20.    


 

3:讀入字符串【杭電2017 字符串統計

輸入數據有多行,第一行是一個整數n,表示測試實例的個數,后面跟着n行,每行包括一個由字母和數字組成的字符串。

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. Sample Input    
  2. 2  
  3. asdfasdf123123asdfasdf  
  4. asdf111111111asdfasdfasdf  
  5.    
  6. import java.util.Scanner;  
  7. public class Main {  
  8. public static void main(String[] args) {  
  9. Scanner sc = new Scanner(System.in);  
  10. int n = sc.nextInt();  
  11. for(int i=0;i<n;i++){  
  12. String str = sc.next();  
  13. ......  
  14. }  
  15. }  
  16. }  
  17. import java.util.Scanner;  
  18. public class Main {  
  19. public static void main(String[] args) {  
  20. Scanner sc = new Scanner(System.in);  
  21. int n = Integer.parseInt(sc.nextLine());  
  22. for(int i=0;i<n;i++){  
  23. String str = sc.nextLine();  
  24. ......  
  25. }  
  26. }  
  27. }  
  28.    


 

3:讀入字符串【杭電2005 第幾天?

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. 給定一個日期,輸出這個日期是該年的第幾天。   
  2. Input  輸入數據有多組,每組占一行,數據格式為YYYY/MM/DD組成  
  3. 1985/1/20  
  4. 2006/3/12  
  5. import java.util.Scanner;  
  6. public class Main {  
  7. public static void main(String[] args) {  
  8. Scanner sc = new Scanner(System.in);  
  9. int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};  
  10. while(sc.hasNext()){  
  11. int days = 0;  
  12. String str = sc.nextLine();  
  13. String[] date = str.split("/");  
  14. int y = Integer.parseInt(date[0]);  
  15. int m = Integer.parseInt(date[1]);  
  16. int d = Integer.parseInt(date[2]);  
  17. if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;  
  18. days += d;  
  19. for(int i=0;i<m;i++){  
  20. days += dd[i];  
  21. }  
  22. System.out.println(days);  
  23. }  
  24. }  
  25. }  


 

 

2. 輸出  

函數:

System.out.print(); 

System.out.println(); 

System.out.format();

System.out.printf();  

 

杭電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

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. import java.util.Scanner;  
  2. public class Main {  
  3. public static void main(String[] args) {  
  4. Scanner sc =new Scanner(System.in);  
  5. int n = sc.nextInt();  
  6. for(int i=0;i<n;i++){  
  7. String op = sc.next();  
  8. int a = sc.nextInt();  
  9. int b = sc.nextInt();  
  10. if(op.charAt(0)=='+'){  
  11. System.out.println(a+b);  
  12. }else if(op.charAt(0)=='-'){  
  13. System.out.println(a-b);  
  14. }else if(op.charAt(0)=='*'){  
  15. System.out.println(a*b);  
  16. }else if(op.charAt(0)=='/'){  
  17. if(a % b == 0) System.out.println(a / b);  
  18. else System.out.format("%.2f", (a / (1.0*b))). Println();  
  19. }  
  20. }  
  21. }  
  22. }  


 

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));

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. public static void main(String[] args) {  
  2.     NumberFormat   formatter   =   new   DecimalFormat( "000000");   
  3.         String  s  =   formatter.format(-1234.567);     //   -001235   
  4.         System.out.println(s);  
  5.         formatter   =   new   DecimalFormat( "##");   
  6.         s   =   formatter.format(-1234.567);             //   -1235   
  7.         System.out.println(s);  
  8.         s   =   formatter.format(0);                      //   0   
  9.         System.out.println(s);  
  10.         formatter   =   new   DecimalFormat( "##00");   
  11.         s   =   formatter.format(0);                     //   00   
  12.         System.out.println(s);  
  13.    
  14.         formatter   =   new   DecimalFormat( ".00");   
  15.         s   =   formatter.format(-.567);               //   -.57   
  16.         System.out.println(s);  
  17.         formatter   =   new   DecimalFormat( "0.00");   
  18.         s   =   formatter.format(-.567);              //   -0.57   
  19.         System.out.println(s);  
  20.         formatter   =   new   DecimalFormat( "#.#");   
  21.         s   =   formatter.format(-1234.567);         //   -1234.6   
  22.         System.out.println(s);  
  23.         formatter   =   new   DecimalFormat( "#.######");   
  24.         s   =   formatter.format(-1234.567);        //   -1234.567   
  25.         System.out.println(s);  
  26.         formatter   =   new   DecimalFormat( ".######");   
  27.         s   =   formatter.format(-1234.567);       //   -1234.567   
  28.         System.out.println(s);  
  29.         formatter   =   new   DecimalFormat( "#.000000");   
  30.         s   =   formatter.format(-1234.567);      //   -1234.567000   
  31.         System.out.println(s);  
  32.           
  33.         formatter   =   new   DecimalFormat( "#,###,###");   
  34.         s   =   formatter.format(-1234.567);      //   -1,235   
  35.         System.out.println(s);  
  36.         s   =   formatter.format(-1234567.890);  //   -1,234,568   
  37.         System.out.println(s);  
  38.    
  39.         //   The   ;   symbol   is   used   to   specify   an   alternate   pattern   for   negative   values   
  40.         formatter   =   new   DecimalFormat( "#;(#) ");   
  41.         s   =   formatter.format(-1234.567);     //   (1235)   
  42.         System.out.println(s);  
  43.    
  44.         //   The   '   symbol   is   used   to   quote   literal   symbols   
  45.         formatter   =   new   DecimalFormat( " '# '# ");   
  46.         s   =   formatter.format(-1234.567);        //   -#1235   
  47.         System.out.println(s);  
  48.         formatter   =   new   DecimalFormat( " 'abc '# ");   
  49.         s   =   formatter.format(-1234.567);      // - abc 1235  
  50.         System.out.println(s);  
  51.    
  52. formatter   =   new   DecimalFormat( "#.##%");   
  53.         s   =   formatter.format(-12.5678987);    
  54.         System.out.println(s);  
  55. }  


 

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. 高精度
BigIntegerBigDecimal可以說是acmer選擇java的首要原因。
函數:add, subtract, divide, mod, compareTo等,其中加減乘除模都要求是BigInteger(BigDecimal)BigInteger(BigDecimal)之間的運算,所以需要把int(double)類型轉換為BigInteger(BigDecimal),用函數BigInteger.valueOf().

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. import java.io.BufferedInputStream;  
  2. import java.math.BigInteger;  
  3. import java.util.Scanner;  
  4. public class Main {  
  5. public static void main(String[] args)   {  
  6. Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  7.         int a = 123, b = 456, c = 7890;  
  8.         BigInteger x, y, z, ans;  
  9.         x = BigInteger.valueOf(a);   
  10.         y = BigInteger.valueOf(b);   
  11.         z = BigInteger.valueOf(c);  
  12.         ans = x.add(y); System.out.println(ans);  
  13.         ans = z.divide(y); System.out.println(ans);  
  14.         ans = x.mod(z); System.out.println(ans);  
  15.         if (ans.compareTo(x) == 0) System.out.println("1");  
  16.     }  
  17. }  


 


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是字符串,basest的進制.
7. 數組排序
函數:Arrays.sort();

 

 

[java]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. public class Main {  
  2. public static void main(String[] args)    {  
  3.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  4.         int n = cin.nextInt();  
  5.         int a[] = new int [n];  
  6.         for (int i = 0; i < n; i++) a[i] = cin.nextInt();  
  7.         Arrays.sort(a);  
  8.         for (int i = 0; i < n; i++) System.out.print(a[i] + " ");  
  9.     }  
  10. }  


易錯:

1.for(int i=m;i<n;i++){isFlowerNum(m);}  //這里m是不變量,應該用i

2.m=m/10的值就變化了如果想要繼續用m,應該提前保存


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM