牛客網(wǎng)OJ

牛客網(wǎng)在線判題系統(tǒng)使用幫助

熟悉目標OJ非常重要,才能保證你做對的代碼運行成功,下面看牛客網(wǎng)OJ:

一、正確處理輸入格式:

常見的輸入格式 應對方法
預先不輸入數(shù)據(jù)的組數(shù) 讀到文件結(jié)尾
預先輸入數(shù)據(jù)的組數(shù) 先讀出組數(shù),然后循環(huán)
只有一組數(shù)據(jù) 直接讀

1. 沒有組數(shù)

Scanner scanner = new Scanner(System.in);
While(scanner.hasNextInt()){
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    System.out.println(a+b);
}

2. 組數(shù) + 循環(huán)輸入

Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    System.out.println(a+b);
}

3. 直接讀

    int a = scanner.nextInt();
    int b = scanner.nextInt();
    System.out.println(a+b);

二、正確處理輸入格式:

看具體要求
不要輸出Case數(shù)
要輸出case數(shù)
每個case之后有空行
兩個case之間有空行

1. 不要輸出Case數(shù)

Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    System.out.println(a+b);
}

2. 要輸出Case數(shù)

Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    System.out.println(“case”+ (i+1)+ “ ”+ (a+b));
}

3. 每個case之后有空行

Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    System.out.println(“case”+ (i+1)+ “ ”+ (a+b)+"\n");
}

4. 兩個case之間有空行

Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    if(i > 0){
        System.out.println(“\n”);
    }
    System.out.println(“case”+ (i+1)+ “ ”+ (a+b));
}

處理細節(jié)和技巧

處理細節(jié)和技巧-輸入
處理細節(jié)和技巧-輸出
image.png
image.png
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容