ACM之Java速成(1)

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

1. 输入:

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

2. 输出

 1 函数:System.out.print(); System.out.println(); System.out.printf();
 2 System.out.print(); // cout << …;
 3 System.out.println(); // cout << … << endl;
 4 System.out.printf(); // 与C中的printf用法类似.
 5 
 6 例程:
 7 import java.io.*;
 8 import java.math.*;
 9 import java.util.*;
10 import java.text.*;
11 
12 public class Main
13 {
14     public static void main(String[] args) 
15     {
16         Scanner cin = new Scanner (new BufferedInputStream(System.in));
17         int a; double b;
18         a = 12345; b = 1.234567;
19         System.out.println(a + " " + b);
20         System.out.printf("%d %10.5f
", a, b); // 输入b为字宽为10,右对齐,保留小数点后5位,四舍五入.
21     }
22 }
23 
24 规格化的输出:
25 函数:
26 // 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.
27     DecimalFormat fd = new DecimalFormat("#.00#");
28     DecimalFormat gd = new DecimalFormat("0.000");
29     System.out.println("x =" + fd.format(x));
30     System.out.println("x =" + gd.format(x));

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

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

4. 高精度
BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

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

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进制表示的字符串

 1 例程:POJ 2305
 2 
 3 
 4 import java.io.*;
 5 import java.util.*;
 6 import java.math.*;
 7 
 8 public class Main
 9 {
10     public static void main(String[] args)
11     {
12         int b;
13         BigInteger p,m,ans;
14         String str ;
15         Scanner cin = new Scanner (new BufferedInputStream(System.in));
16         while(cin.hasNext())
17         {
18             b=cin.nextInt();
19             if(b==0)
20                 break;
21             p=cin.nextBigInteger(b);
22             m=cin.nextBigInteger(b);
23             ans=p.mod(m);
24             str=ans.toString(b);
25             System.out.println(str);
26         }
27     }
28 }


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

 1 例程:
 2 import java.io.*;
 3 import java.math.*;
 4 import java.util.*;
 5 import java.text.*;
 6 
 7 public class Main
 8 {
 9     public static void main(String[] args) 
10     {
11         Scanner cin = new Scanner (new BufferedInputStream(System.in));
12         int n = cin.nextInt();
13         int a[] = new int [n];
14         for (int i = 0; i < n; i++) a[i] = cin.nextInt();
15         Arrays.sort(a);
16         for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
17     }
18 }

7. POJ高精度题目汇总:
POJ 1131 1205 1220 1405 1503 1604 1894 2084 2305 2325 2389 2413 3101 3199

未完待续,,,

 

原文地址:https://www.cnblogs.com/jeff-wgc/p/4480016.html