Java for Acm

这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目。


1. 输入:

格式为:Scanner cin = new Scanner (new BufferedInputStream(System.in));

例:

  1. import java.io.*;  
  2.   
  3. import java.math.*;  
  4.   
  5. import java.util.*;  
  6.   
  7. import java.text.*;  
  8.   
  9. public classMain  
  10.   
  11. {  
  12.   
  13.     public static void main(String[] args)  
  14.   
  15.     {  
  16.   
  17.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  18.   
  19.         int a; double b; BigInteger c; String st;  
  20.   
  21.         a = cin.nextInt(); b = cin.nextDouble(); c = cin.nextBigInteger(); d = cin.nextLine(); // 每种类型都有相应的输入函数.  
  22.   
  23.     }  
  24.   
  25. }  


2. 输出

函数:System.out.print(); System.out.println(); System.out.printf();

System.out.print(); // cout << …;

System.out.println(); // cout << … << endl;

System.out.printf(); // 与C中的printf用法类似.

例:

  1. import java.io.*;  
  2.   
  3. import java.math.*;  
  4.   
  5. import java.util.*;  
  6.   
  7. import java.text.*;  
  8.   
  9. public classMain  
  10.   
  11. {  
  12.   
  13.     public static void main(String[] args)  
  14.   
  15.     {  
  16.   
  17.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  18.   
  19.         int a; double b;  
  20.   
  21.         a = 12345; b = 1.234567;  
  22.   
  23.         System.out.println(a + " " + b);  
  24.   
  25.         System.out.printf("%d %10.5f\n", a, b); // 输入b为字宽为10,右对齐,保留小数点后5位,四舍五入.  
  26.   
  27.     }  
  28.   
  29. }  


规格化的输出:

函数:

// 这里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));


3. 字符串处理

java中字符串String是不可以修改的,要修改只能转换为字符数组.

例程:

  1. import java.io.*;  
  2.   
  3. import java.math.*;  
  4.   
  5. import java.util.*;  
  6.   
  7. import java.text.*;  
  8.   
  9. public classMain  
  10.   
  11. {  
  12.   
  13.     public static void main(String[] args)  
  14.   
  15.     {  
  16.   
  17.         int i;  
  18.   
  19.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  20.   
  21.         String st = "abcdefg";  
  22.   
  23.         System.out.println(st.charAt(0)); // st.charAt(i)就相当于st[i].  
  24.   
  25.         char [] ch;  
  26.   
  27.         ch = st.toCharArray(); // 字符串转换为字符数组.  
  28.   
  29.         for (i = 0; i < ch.length; i++) ch[i] += 1;  
  30.   
  31.         System.out.println(ch); // 输入为“bcdefgh”.  
  32.   
  33. if (st.startsWith("a")) // 如果字符串以'0'开头.  
  34.   
  35.         {  
  36.   
  37.             st = st.substring(1); // 则从第1位开始copy(开头为第0位).  
  38.   
  39.         }  
  40.   
  41.     }  
  42.   
  43. }  

4. 高精度

BigInteger和BigDecimal可以说是acmer选择java的首要原因。

函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

例程:

  1. import java.io.*;  
  2.   
  3. import java.math.*;  
  4.   
  5. import java.util.*;  
  6.   
  7. import java.text.*;  
  8.   
  9. public classMain  
  10.   
  11. {  
  12.   
  13.     public static void main(String[] args)  
  14.   
  15.     {  
  16.   
  17.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  18.   
  19.         int a = 123, b = 456, c = 7890;  
  20.   
  21.         BigInteger x, y, z, ans;  
  22.   
  23.         x = BigInteger.valueOf(a); y = BigInteger.valueOf(b); z = BigInteger.valueOf(c);  
  24.   
  25.         ans = x.add(y); System.out.println(ans);  
  26.   
  27.         ans = z.divide(y); System.out.println(ans);  
  28.   
  29.         ans = x.mod(z); System.out.println(ans);  
  30.   
  31.         if (ans.compareTo(x) == 0) System.out.println("1");  
  32.   
  33.     }  
  34.   
  35. }  


5. 进制转换

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的进制.

//Added by abilitytao

1.如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2);

当然也可以使用其他进制方式读入;

2.如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);//将它转换成2进制表示的字符串

例程:POJ 2305

  1. import java.io.*;  
  2.   
  3. import java.util.*;  
  4.   
  5. import java.math.*;  
  6.   
  7. public classMain  
  8.   
  9. {  
  10.   
  11.     public static void main(String[] args)  
  12.   
  13.     {  
  14.   
  15.         int b;  
  16.   
  17.         BigInteger p,m,ans;  
  18.   
  19.         String str ;  
  20.   
  21.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  22.   
  23.         while(cin.hasNext())  
  24.   
  25.         {  
  26.   
  27.             b=cin.nextInt();  
  28.   
  29.             if(b==0)  
  30.   
  31.                 break;  
  32.   
  33.             p=cin.nextBigInteger(b);  
  34.   
  35.             m=cin.nextBigInteger(b);  
  36.   
  37.             ans=p.mod(m);  
  38.   
  39.             str=ans.toString(b);  
  40.   
  41.             System.out.println(str);  
  42.   
  43.         }  
  44.   
  45.     }  
  46.   
  47. }  


6. 排序

函数:Arrays.sort();至于怎么排序结构体,像C++里写个cmp的方法,在java还不太清楚,希望有人指点下~~

例程:

  1. import java.io.*;  
  2.   
  3. import java.math.*;  
  4.   
  5. import java.util.*;  
  6.   
  7. import java.text.*;  
  8.   
  9. public classMain  
  10.   
  11. {  
  12.   
  13.     public static void main(String[] args)  
  14.   
  15.     {  
  16.   
  17.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  18.   
  19.         int n = cin.nextInt();  
  20.   
  21.         int a[] = new int [n];  
  22.   
  23.         for (int i = 0; i < n; i++) a[i] = cin.nextInt();  
  24.   
  25.         Arrays.sort(a);  
  26.   
  27.         for (int i = 0; i < n; i++) System.out.print(a[i] + " ");  
  28.   
  29.     }  
  30.   
  31. }  


7. POJ高精度题目汇总:

POJ 1131 1205 1220 1405 1503 1604 1894 2084 2305 2325 2389 2413 3101 3199

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值