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

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

一. 前言

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

二. 例子 (1)输入输出a+b
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。输出a+b的结果。

输入

1 5
10 20

输出

6
30

解答:

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 b = in.nextInt();
            System.out.println(a+b);
        }
    }
}

上述方法是常规方法,耗时71ms

使用io读取的话,能提高很多时间,只要10ms

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main{
    public static void main(String[] strr) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while((str=br.readLine())!=null){
            String[] nums = str.split(" ");
            if(nums.length == 2){
                int a = Integer.parseInt(nums[0]);
                int b = Integer.parseInt(nums[1]);
                System.out.println(a + b);
            }
        }
    }
}
//from 牛客 47128143	5850710
(2)输入输出a+b+c+...

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

输入:

1 2 3
4 5
0 0 0 0 0

输出:

6
9
0

import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args){
         Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int sum = 0;
            String[] str = in.nextLine().split(" ");
            for(String s : str){
                sum += Integer.parseInt(s);
            }
            System.out.println(sum);
        }
    }
}

io版本

import java.util.Scanner;
import java.io.*;
public class Main{
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while((str=in.readLine())!=null){
            String[] nums = str.split(" ");
            int sum = 0;
            for(int i = 0;i< nums.length;i++){
                sum += Integer.parseInt(nums[i]);
            }
            System.out.println(sum);
        }
         
    }
}
//from 牛客 牛客564602593号
(3)输入输出a+b(遇到0结束)、

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

输出a+b的结果

输入:

1 5
10 20
0 0

输出:

6
30

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

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--;
        }
    }
}
//from 牛客 440419586号 

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存