ACM orブルーブリッジカップのJava入出力関連


一、注意点
    1. クラス名はpublic class Mainで命名する必要があります
    2. 一部のOJシステムでは、出力の末尾に「」が1つ増えても、プログラムがエラーを出力する可能性があります.
    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()) 
		 
		
	}

}

    
5.システムについてnanoTime()
関数の使用
使用可能なシステムタイマの現在の値をミリ秒単位で最も正確に返します.
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入力データには複数のグループがあり、各グループが1行を占め、整数で構成されています. 
Sample Input 
56
67
100
123 
import java.util.Scanner;  
  
public class Main {  
    public static void main(String[] args) {  
        Scanner in = new Scanner(System.in);  
        while (in.hasNext()) { //         
            int num = in.nextInt();//        
        }  
    }  
}  
例2:実数を読み込む
 
入力データには複数のグループがあり、各グループは2行を占め、第1の動作は整数Nであり、第2の行がN個の実数を含むことを示す.
Sample Input

56.9  67.7  90.5  12.8 

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文字列統計】入力データは複数行あり、第1行は整数nであり、テスト例の個数を表し、後にn行が続く.各行にはアルファベットと数字からなる文字列が含まれる.Sample Input 2 asdfasdf 23123 asdfasdf asdf 11111 asdfasdfasdf
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入力データは複数のグループがあり、各グループは1行を占め、データフォーマットはYYY/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);  
        }  
    }  
}  

2.出力関数:System.out.print();  System.out.println();  System.out.format(); System.out.printf();  
3.正規化出力:関数://ここで0は1桁の数字、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' 
サブストリング法を用いてサブストリングを得ることができ、上述の例では
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
2つのパラメータがあります
,
最初は回転する文字列
,
2つ目は、進数を説明します.
).  
BigInter m = new BigInteger(st, base); // st
文字列です.
base
はい
st
の進数
.
7.配列ソート関数:Arrays.sort();
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] + " ");  
    }  
}