栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一. 前言

        在面试或者是做牛客的模式时,回经常遇到输入输出自己写的情况,因此是非常必要掌握输入输出的一些操作。牛客网上也有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号 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/825603.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号