前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【经验总结】Java在ACM算法竞赛编程中易错点

【经验总结】Java在ACM算法竞赛编程中易错点

作者头像
Angel_Kitty
发布2018-04-09 15:34:06
1.4K0
发布2018-04-09 15:34:06
举报

一、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. 输入:

格式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:读入整数

  1. Input  输入数据有多组,每组占一行,由一个整数组成。   
  2. Sample Input   
  3. 56
  4. 67
  5. 100
  6. 123
  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 score = sc.nextInt();//读入整数
  13. 。。。。  
  14. }  
  15. }  
  16. }  

例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. import java.util.Scanner;  
  7. public class Main {  
  8. public static void main(String[] args) {  
  9. Scanner sc =new Scanner(System.in);  
  10. while(sc.hasNext()){  
  11. int n = sc.nextInt();  
  12. for(int i=0;i<n;i++){  
  13. double a = sc.nextDouble();  
  14. 。。。。。。  
  15. }  
  16. }  
  17. }  
  18. }  

例3:读入字符串【杭电2017 字符串统计】

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

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

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

例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

  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.         formatter   =   new   DecimalFormat( ".00");   
  14.         s   =   formatter.format(-.567);               //   -.57 
  15.         System.out.println(s);  
  16.         formatter   =   new   DecimalFormat( "0.00");   
  17.         s   =   formatter.format(-.567);              //   -0.57 
  18.         System.out.println(s);  
  19.         formatter   =   new   DecimalFormat( "#.#");   
  20.         s   =   formatter.format(-1234.567);         //   -1234.6 
  21.         System.out.println(s);  
  22.         formatter   =   new   DecimalFormat( "#.######");   
  23.         s   =   formatter.format(-1234.567);        //   -1234.567 
  24.         System.out.println(s);  
  25.         formatter   =   new   DecimalFormat( ".######");   
  26.         s   =   formatter.format(-1234.567);       //   -1234.567 
  27.         System.out.println(s);  
  28.         formatter   =   new   DecimalFormat( "#.000000");   
  29.         s   =   formatter.format(-1234.567);      //   -1234.567000 
  30.         System.out.println(s);  
  31.         formatter   =   new   DecimalFormat( "#,###,###");   
  32.         s   =   formatter.format(-1234.567);      //   -1,235 
  33.         System.out.println(s);  
  34.         s   =   formatter.format(-1234567.890);  //   -1,234,568 
  35.         System.out.println(s);  
  36. //   The   ;   symbol   is   used   to   specify   an   alternate   pattern   for   negative   values 
  37.         formatter   =   new   DecimalFormat( "#;(#) ");   
  38.         s   =   formatter.format(-1234.567);     //   (1235) 
  39.         System.out.println(s);  
  40. //   The   '   symbol   is   used   to   quote   literal   symbols 
  41.         formatter   =   new   DecimalFormat( " '# '# ");   
  42.         s   =   formatter.format(-1234.567);        //   -#1235 
  43.         System.out.println(s);  
  44.         formatter   =   new   DecimalFormat( " 'abc '# ");   
  45.         s   =   formatter.format(-1234.567);      // - abc 1235
  46.         System.out.println(s);  
  47. formatter   =   new   DecimalFormat( "#.##%");   
  48.         s   =   formatter.format(-12.5678987);    
  49.         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是字符串,base是st的进制. 7. 数组排序 函数:Arrays.sort();

5. 高精度 BigInteger和BigDecimal可以说是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上查看代码片
在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上查看代码片
在CODE上查看代码片
派生到我的代码片
派生到我的代码片
  1. long startTime = System.nanoTime();  
  2. // ... the code being measured ...
  3. 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:读入整数

[java] view plain copy     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();//读入整数   。。。。   }   }   }

  1. Input  输入数据有多组,每组占一行,由一个整数组成。   
  2. Sample Input   
  3. 56
  4. 67
  5. 100
  6. 123
  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 score = sc.nextInt();//读入整数
  13. 。。。。  
  14. }  
  15. }  
  16. }  

例2:读入实数

输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数。

[java] view plain copy     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();   。。。。。。   }   }   }   }

  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. import java.util.Scanner;  
  7. public class Main {  
  8. public static void main(String[] args) {  
  9. Scanner sc =new Scanner(System.in);  
  10. while(sc.hasNext()){  
  11. int n = sc.nextInt();  
  12. for(int i=0;i<n;i++){  
  13. double a = sc.nextDouble();  
  14. 。。。。。。  
  15. }  
  16. }  
  17. }  
  18. }  

例3:读入字符串【杭电2017 字符串统计】

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

[java] view plain copy     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();   ......   }   }   }

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

例3:读入字符串【杭电2005 第几天?】

[java] view plain copy     给定一个日期,输出这个日期是该年的第几天。    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);   }   }   }

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

例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

[java] view plain copy     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();   }   }   }   }

  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     public static void main(String[] args) {       NumberFormat   formatter   =   new   DecimalFormat( "000000");            String  s  =   formatter.format(-1234.567);     //   -001235            System.out.println(s);           formatter   =   new   DecimalFormat( "##");            s   =   formatter.format(-1234.567);             //   -1235            System.out.println(s);           s   =   formatter.format(0);                      //   0            System.out.println(s);           formatter   =   new   DecimalFormat( "##00");            s   =   formatter.format(0);                     //   00            System.out.println(s);               formatter   =   new   DecimalFormat( ".00");            s   =   formatter.format(-.567);               //   -.57            System.out.println(s);           formatter   =   new   DecimalFormat( "0.00");            s   =   formatter.format(-.567);              //   -0.57            System.out.println(s);           formatter   =   new   DecimalFormat( "#.#");            s   =   formatter.format(-1234.567);         //   -1234.6            System.out.println(s);           formatter   =   new   DecimalFormat( "#.######");            s   =   formatter.format(-1234.567);        //   -1234.567            System.out.println(s);           formatter   =   new   DecimalFormat( ".######");            s   =   formatter.format(-1234.567);       //   -1234.567            System.out.println(s);           formatter   =   new   DecimalFormat( "#.000000");            s   =   formatter.format(-1234.567);      //   -1234.567000            System.out.println(s);                      formatter   =   new   DecimalFormat( "#,###,###");            s   =   formatter.format(-1234.567);      //   -1,235            System.out.println(s);           s   =   formatter.format(-1234567.890);  //   -1,234,568            System.out.println(s);               //   The   ;   symbol   is   used   to   specify   an   alternate   pattern   for   negative   values            formatter   =   new   DecimalFormat( "#;(#) ");            s   =   formatter.format(-1234.567);     //   (1235)            System.out.println(s);               //   The   '   symbol   is   used   to   quote   literal   symbols            formatter   =   new   DecimalFormat( " '# '# ");            s   =   formatter.format(-1234.567);        //   -#1235            System.out.println(s);           formatter   =   new   DecimalFormat( " 'abc '# ");            s   =   formatter.format(-1234.567);      // - abc 1235           System.out.println(s);       formatter   =   new   DecimalFormat( "#.##%");            s   =   formatter.format(-12.5678987);             System.out.println(s);   }

  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.         formatter   =   new   DecimalFormat( ".00");   
  14.         s   =   formatter.format(-.567);               //   -.57 
  15.         System.out.println(s);  
  16.         formatter   =   new   DecimalFormat( "0.00");   
  17.         s   =   formatter.format(-.567);              //   -0.57 
  18.         System.out.println(s);  
  19.         formatter   =   new   DecimalFormat( "#.#");   
  20.         s   =   formatter.format(-1234.567);         //   -1234.6 
  21.         System.out.println(s);  
  22.         formatter   =   new   DecimalFormat( "#.######");   
  23.         s   =   formatter.format(-1234.567);        //   -1234.567 
  24.         System.out.println(s);  
  25.         formatter   =   new   DecimalFormat( ".######");   
  26.         s   =   formatter.format(-1234.567);       //   -1234.567 
  27.         System.out.println(s);  
  28.         formatter   =   new   DecimalFormat( "#.000000");   
  29.         s   =   formatter.format(-1234.567);      //   -1234.567000 
  30.         System.out.println(s);  
  31.         formatter   =   new   DecimalFormat( "#,###,###");   
  32.         s   =   formatter.format(-1234.567);      //   -1,235 
  33.         System.out.println(s);  
  34.         s   =   formatter.format(-1234567.890);  //   -1,234,568 
  35.         System.out.println(s);  
  36. //   The   ;   symbol   is   used   to   specify   an   alternate   pattern   for   negative   values 
  37.         formatter   =   new   DecimalFormat( "#;(#) ");   
  38.         s   =   formatter.format(-1234.567);     //   (1235) 
  39.         System.out.println(s);  
  40. //   The   '   symbol   is   used   to   quote   literal   symbols 
  41.         formatter   =   new   DecimalFormat( " '# '# ");   
  42.         s   =   formatter.format(-1234.567);        //   -#1235 
  43.         System.out.println(s);  
  44.         formatter   =   new   DecimalFormat( " 'abc '# ");   
  45.         s   =   formatter.format(-1234.567);      // - abc 1235
  46.         System.out.println(s);  
  47. formatter   =   new   DecimalFormat( "#.##%");   
  48.         s   =   formatter.format(-12.5678987);    
  49.         System.out.println(s);  
  50. }  

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().

[java] view plain copy     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");       }   }

  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是字符串,base是st的进制. 7. 数组排序 函数:Arrays.sort();

[java] view plain copy     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] + " ");       }   }

  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,应该提前保存

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 二、Java之输入输出处理
  • 二、Java之输入输出处理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档