Java-输入输出专场(牛客面试)<二>

Java-输入输出专场(牛客面试)<二>,第1张

一. 前言

在面试或者是做牛客的模式时,回经常遇到输入输出自己写的情况,因此是非常必要掌握输入输出的一些 *** 作。牛客网上也有OJ在线编程常见输入输出练习场,非常值得一看。下面是一些例子。

Java-输入输出专场(牛客/面试)<一>

Java-输入输出专场(牛客/面试)<一>_FordwardSummer的博客-CSDN博客
Java-输入输出专场(牛客/面试)<二>

Java-输入输出专场(牛客/面试)<二>_FordwardSummer的博客-CSDN博客
Java-输入输出专场(牛客/面试)<三>

Java-输入输出专场(牛客/面试)<三>_FordwardSummer的博客-CSDN博客

二. 例子 (4)输入输出a+b,指定行数

输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)

输出a+b的结果

例:

输入:

2
1 5
10 20

输出:

6
30

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

io版本

import java.io.*;
 
public class Main{
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] s;
        while(n > 0){
            s = br.readLine().split(" ");
            int a = Integer.parseInt(s[0]);
            int b = Integer.parseInt(s[1]);
            System.out.println(a + b);
            n--;
        }
    }
}
(5)输入输出a+b+c...,遇到0结束

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

每组数据输出求和的结果

例:

输入:

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

输出:

10
15

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(true){
            int a = in.nextInt();
            if(a == 0) break;
            int sum = 0;
            while(a>0){
                int b = in.nextInt();
                sum += b;
                a--;
            }
            System.out.println(sum);
        }
    }
}

io版本 

import java.io.*;
public class Main {
     public static void main (String args[]) throws IOException{
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         String s[];
         while(true){
             s=br.readLine().split(" ");
             if(Integer.parseInt(s[0])==0)
                 break;
             int sum=0;
             for(int i=1;i
(6)输入输出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.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        while(num > 0){
            int a = in.nextInt();
            int sum = 0;
            while(a > 0){
                int b = in.nextInt();
                sum += b;
                a--;
            }
            System.out.println(sum);
            num--;
        }
    }
}

io版本

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        String[] temp = str.split(" ");
        while((str = br.readLine())!=null){
            temp = str.split(" ");
            int res = 0;
            for(int i=1; i
(7)输入输出a+b+c...,不指每行个数

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

例如:

输入:

4 1 2 3 4
5 1 2 3 4 5

输出:

10

15

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

io版本

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/742364.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)