牛客Java笔试必备 - 输入输出流整理

没有风暴,帆船不过是一块破布。

1. A+B 多次输入多组数据   

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。输出a+b

输入:
1 2
3 4
输出:
3
7
import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        int a,b;
        while(in.hasNext()){
            a = in.nextInt();
            b = in.nextInt();
            System.out.println(a+b);
        }
    }
}

 链接:https://ac.nowcoder.com/acm/contest/5649/A?&headNav=acm

2. A+B  一次输入指定组数据   

输入第一行包括一个数据组数t(1 <= t <= 100)。 

接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)。

输出a+b的结果

输入:
2
1 5
10 20
输出:
6
30
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++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a+b);
        }
    }
}

3. A+B 指定字符输入停止  

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0则结束输入.   

输出a+b的结果

输入:
1 5
10 20
0 0
输出:
6
30
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();
            if(a==0&&b==0){
                break;
            }
            
            System.out.println(a+b);
        }      
    }
}

  4. A+B+C 多组输入指定字符停止

输入数据包括多组。每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。接下来n个正整数,即需要求和的每个正整数。

输出:每组数据求和结果

输入
4 1 2 3 4
5 1 2 3 4 5
0
输出
10
15
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 sum = 0;
            if(a == 0) return;
            for(int i = 0; i < a; i++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

 5. A+B+C 指定组数据指定个数和

输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输入
2
4 1 2 3 4
5 1 2 3 4 5
输出
10
15
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++){
            int m = sc.nextInt();
            int sum = 0;
            for(int j = 0; j < m; j++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

 6. A+B+C 多组输入不暂停

输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输入
4 1 2 3 4
5 1 2 3 4 5
输出
10
15
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int sum = 0;
            int n = sc.nextInt();
            for(int i = 0; i < n; i++){
                int a = sc.nextInt();
                sum += a;
            }
            System.out.println(sum);
        }
    }
}

 7. A+B+C+... 多组输入不定项求和

输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。

输入
1 2 3
4 5
0 0 0 0 0
输出
6
9
0

思路:直接读入一行数据进行处理

import java.util.Scanner;
public class Main{
    public static void main(String[] argd){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            int sum = 0;
            String[] s = sc.nextLine().split(" ");
            for(int i = 0; i < s.length; i++){
                sum += Integer.valueOf(s[i]);   //parseInt
            }
            System.out.println(sum);
        }
    }
}

8. 字符串排序 一次输入多个字符串

输入有两行,第一行n。 第二行是n个空格隔开的字符串。     输出一行排序后的字符串,空格隔开,无结尾空格

输入:
5
c d a bb e
输出
a bb c d e

使用nextLine()  split()

import java.util.*;
public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        sc.nextLine();
        String[] str = sc.nextLine().split(" ");
        Arrays.sort(str);
        for(int i = 0; i < str.length; i++){
            System.out.print(str[i]+" ");
        }
    }
}

9. 字符串排序 多组输入

多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
输入
a c bb
f dddd
nowcoder
输出
a bb c
dddd f
nowcoder

 

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白Rachel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值