天天看點

20210907 線上程式設計常見輸入輸出練習

20210907線上程式設計常見輸入輸出練習

編輯時間:2021/09/07

讀完本節:大概花費15分鐘,共2102詞

文章目錄

    • 20210907線上程式設計常見輸入輸出練習
      • 例1. a+b 輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料包括多組。
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例2. a+b 輸入第一行包括一個資料組數t(1 <= t <= 100)接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例3. a+b 輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料有多組, 如果輸入為0 0則結束輸入
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例4. a+b 每組資料一行,每行的第一個整數為整數的個數n(1 <= n <= 100), n為0的時候結束輸入,接下來n個正整數,即需要求和的每個正整數。
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例5. a+b 輸入的第一行包括一個正整數t(1 <= t <= 100), 表示資料組數。接下來t行, 每行一組資料。接下來n個正整數, 即需要求和的每個正整數。
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例6. a+b 輸入資料有多組, 每行表示一組輸入資料。每行的第一個整數為整數的個數n(1 <= n <= 100)。接下來n個正整數, 即需要求和的每個正整數。
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例7. 輸入資料有多組, 每行表示一組輸入資料。每行不定有n個整數,空格隔開。(1 <= n <= 100)。
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例8. 對輸入的字元串進行排序後輸出
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例9. 對輸入的字元串進行排序後輸.出多個測試用例,每個測試用例一行。每行通過空格隔開,有n個字元,n<100
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例10. 對輸入的字元串進行排序後輸.出多個測試用例,每個測試用例一行。每行通過`,`隔開,有n個字元,n<100
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
      • 例11. 傳回通過率0
        • 1. 輸入描述:
        • 2. 輸出描述:
        • 3. 測試用例
        • 4. 代碼
            • `無限進步`

例1. a+b 輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料包括多組。

1. 輸入描述:

​ 輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料包括多組。

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

1 5
10 20
           

​ 輸出

6
30
           

4. 代碼

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}
           

例2. a+b 輸入第一行包括一個資料組數t(1 <= t <= 100)接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)

1. 輸入描述:

​ 輸入第一行包括一個資料組數t(1 <= t <= 100)

​ 接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

2
1 5
10 20
           

​ 輸出

6
30
           

4. 代碼

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int groupNum = sc.nextInt();
        while(groupNum > 0){
            long a = sc.nextLong();
            long b = sc.nextLong();
            System.out.println(a + b);
            groupNum--;
        }
    }
}
           

例3. a+b 輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料有多組, 如果輸入為0 0則結束輸入

1. 輸入描述:

​ 輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料有多組, 如果輸入為0 0則結束輸入

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

1 5
10 20
0 0
           

​ 輸出

6
30
           

4. 代碼

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long a = sc.nextLong();
            long b = sc.nextLong();
            if(a == 0l && b == 0l)    break;
            System.out.println(a + b);
        }
    }
}
           

例4. a+b 每組資料一行,每行的第一個整數為整數的個數n(1 <= n <= 100), n為0的時候結束輸入,接下來n個正整數,即需要求和的每個正整數。

1. 輸入描述:

​ 輸入資料包括多組。

​ 每組資料一行,每行的第一個整數為整數的個數n(1 <= n <= 100), n為0的時候結束輸入。

​ 接下來n個正整數,即需要求和的每個正整數。

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

4 1 2 3 4
5 1 2 3 4 5
0
           

​ 輸出

10
15
           

4. 代碼

import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine()) != null) {
            String[] str = line.split(" ");
            int n = Integer.parseInt(str[0]);
            if (n == 0) {
                break;
            }
            int sum = 0;
            // 如果不用str.length,而用n,結束條件多個=
            // 因為str.length = n+1,第一個正數個數和後面n個正整數
            for (int i = 1; i <= n; i++) {
                sum += Integer.parseInt(str[i]);
            }
            System.out.println(sum);
        }
        br.close();
    }
}
           

例5. a+b 輸入的第一行包括一個正整數t(1 <= t <= 100), 表示資料組數。接下來t行, 每行一組資料。接下來n個正整數, 即需要求和的每個正整數。

1. 輸入描述:

​ 輸入的第一行包括一個正整數t(1 <= t <= 100), 表示資料組數。

​ 接下來t行, 每行一組資料。

​ 每行的第一個整數為整數的個數n(1 <= n <= 100)。

​ 接下來n個正整數, 即需要求和的每個正整數。

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

2
4 1 2 3 4
5 1 2 3 4 5
           

​ 輸出

10
15
           

4. 代碼

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int groupNum = Integer.parseInt(br.readLine());
        while(groupNum > 0){
            String[] str = br.readLine().split(" ");
            int n = Integer.parseInt(str[0]);
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                sum += Integer.parseInt(str[i]);
            }
            System.out.println(sum);
            groupNum--;
        }
        br.close();
    }
}
           

例6. a+b 輸入資料有多組, 每行表示一組輸入資料。每行的第一個整數為整數的個數n(1 <= n <= 100)。接下來n個正整數, 即需要求和的每個正整數。

1. 輸入描述:

​ 輸入資料有多組, 每行表示一組輸入資料。

​ 每行的第一個整數為整數的個數n(1 <= n <= 100)。

​ 接下來n個正整數, 即需要求和的每個正整數。

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

4 1 2 3 4
5 1 2 3 4 5
           

​ 輸出

10
15
           

4. 代碼

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line=br.readLine()) != null){
            String[] str = line.split(" ");
            int range = Integer.parseInt(str[0]);
            int sum = 0;
            for(int i = 1; i <= range; i++){
                sum += Integer.parseInt(str[i]);
            }
            System.out.println(sum);
        }
        br.close();
    }
}
           

例7. 輸入資料有多組, 每行表示一組輸入資料。每行不定有n個整數,空格隔開。(1 <= n <= 100)。

1. 輸入描述:

​ 輸入資料有多組, 每行表示一組輸入資料。

​ 每行不定有n個整數,空格隔開。(1 <= n <= 100)。

2. 輸出描述:

​ 輸出a+b的結果

3. 測試用例

​ 輸入

1 2 3
4 5
0 0 0 0 0
           

​ 輸出

6
9
0
           

4. 代碼

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            String[] str = line.split(" ");
            int sum = 0;
            for(int i = str.length - 1; i >= 0; --i){
                sum += Integer.parseInt(str[i]);
            }
            System.out.println(sum);
        }
        br.close();
    }
}
           

例8. 對輸入的字元串進行排序後輸出

1. 輸入描述:

​ 輸入有兩行,第一行n

​ 第二行是n個空格隔開的字元串

2. 輸出描述:

​ 輸出一行排序後的字元串,空格隔開,無結尾空格

3. 測試用例

​ 輸入

5
c d a bb e
           

​ 輸出

a bb c d e
           

4. 代碼

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        line = br.readLine();
        String[] arr = line.split(" ");
        Arrays.sort(arr);
        for(int i = 0; i < arr.length - 1; ++i){
            System.out.print(arr[i] + " ");
        }
        System.out.print(arr[arr.length - 1]);
        br.close();
    }
}
           

例9. 對輸入的字元串進行排序後輸.出多個測試用例,每個測試用例一行。每行通過空格隔開,有n個字元,n<100

1. 輸入描述:

​ 多個測試用例,每個測試用例一行。

​ 每行通過空格隔開,有n個字元,n<100

2. 輸出描述:

​ 對于每組測試用例,輸出一行排序過的字元串,每個字元串通過空格隔開

3. 測試用例

​ 輸入

a c bb
f dddd
nowcoder
           

​ 輸出

a bb c
dddd f
nowcoder
           

4. 代碼

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            String[] arr = line.split(" ");
            Arrays.sort(arr);
            for(int i = 0; i < arr.length - 1; i++){
                System.out.print(arr[i] + " ");
            }
            System.out.print(arr[arr.length - 1]);
            System.out.println();
        }
        br.close();
    }
}
           

例10. 對輸入的字元串進行排序後輸.出多個測試用例,每個測試用例一行。每行通過

,

隔開,有n個字元,n<100

1. 輸入描述:

​ 多個測試用例,每個測試用例一行。

​ 每行通過

,

隔開,有n個字元,n<100

2. 輸出描述:

​ 對于每組測試用例,輸出一行排序過的字元串,每個字元串通過

,

隔開

3. 測試用例

​ 輸入

a,c,bb
f,dddd
nowcoder
           

​ 輸出

a,bb,c
dddd,f
nowcoder
           

4. 代碼

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            String[] arr = line.split(",");
            Arrays.sort(arr);
            for(int i = 0; i < arr.length - 1; i++){
                System.out.print(arr[i] + ",");
            }
            System.out.print(arr[arr.length - 1]);
            System.out.println();
        }
        br.close();
    }
}
           

例11. 傳回通過率0

1. 輸入描述:

​ 輸入有多組測試用例,每組空格隔開兩個整數

2. 輸出描述:

​ 對于每組資料輸出一行兩個整數的和

3. 測試用例

​ 輸入

123
100 100
10000000000000 1
           

​ 輸出

123
200
10000000000001
           

4. 代碼

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line=br.readLine()) != null){
            String[] str = line.split(" ");
            long sum = 0;
            for(String i:str){
                sum += Long.parseLong(i);
            }
            System.out.println(sum);
        } 
    }
}
           

送出不通過,或者通過率為零,這不是系統的錯,可能是因為

1.對題目了解錯了,代碼隻過了樣例或你自己的資料

2.代碼邏輯有問題,代碼隻過了樣例或你自己的資料

總之就是你的代碼隻是過了樣例和自測資料,背景的測試資料你根本不可見,要多自己思考。

無限進步