轉載自:http://blog.csdn.net/Chen_Tongsheng/article/details/53354169
一、注意點
1. 類名稱必須采用public class Main方式命名
2. 在有些OJ系統上,即便是輸出的末尾多了一個“ ”,程序可能會輸出錯誤
3. 有些OJ上的題目會直接將OI上的題目拷貝過來,所以即便是題目中有輸入和輸出文件,可能也不需要,因為在OJ系統中一般是采用標准輸入輸出,不需要文件
4. 在有多行數據輸入的情況下,一般按以下代碼示例處理:
import java.util.Scanner; import java.io.*; public class Main{ public static void main(String[] args){ Scanner in1 = new Scanner(System.in); Scanner in2 = new Scanner(new BufferedInputStream(System.in));//輸入速度較前者 in1 要快,但但一般不用這個BufferedInputStream緩存 //輸入多組測試數據一般用:while(in1.hasNextInt()) 或者是 while(in1.hasNext()) } }
long startTime = System.nanoTime(); // Code... 運用算法來解決問題的運行代碼 long estimatedTime = System.nanoTime() - startTime;
二、輸入輸出處理
由於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
例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 n = Integer.parseInt(sc.nextLine()); for (int i = 0; i < n; i++) { String str = sc.nextLine(); } } }
2. 輸出
函數:
System.out.print();
System.out.println();
System.out.format();
System.out.printf();
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));
import java.util.Scanner; public class Main { 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); } }
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();