OJ在线编程常见输入输出练习

OJ在线编程常见输入输出练习

题目来自牛客网

这篇是为了第一次接触OJ不太明白该如何弄输入输出的小伙伴准备的

  • 说的再多也不如去leetcode上死磕
  • 建议5-10分钟没有思路就去读答案
  • 但是读完要多敲几遍
  • AC之后再去英文站读读description 和 solution

IDEA 和 VS Code都支持lettcode插件了!!!用着贼爽!!!

(1)A+B

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

示例1

输入
1 5
10 20
输出
6
30
import java.util.*;

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)
输出描述:
输出a+b的结果

示例1

输入
2
1 5
10 20
输出
6
30
import java.util.*;

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

(3)A+B

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

示例1

输入
1 5
10 20
0 0
输出
6
30
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        while(true) {
            int a = s.nextInt();
            int b = s.nextInt();
            if (a == 0 && b == 0) {//如果输入为0,0则结束输入
                break;
            }else {
                System.out.println(a + b);
            }
        }
    }
}

(4)A+B

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

示例1

输入
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 sc = new Scanner(System.in);
    while(!sc.hasNext("0")){
        int num = sc.nextInt(); int sum = 0;
        for(int i =0 ; i< num; i ++){
            sum+=sc.nextInt();
        }
        System.out.println(sum);
    }
  }
}

(5)A+B

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

示例1

输入
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 sc=new Scanner(System.in);
        int h=sc.nextInt();
          
        for(int i=0;i<h;i++){
            int g=sc.nextInt();
            int sum=0;
            for(int j=0;j<g;j++){
                sum+=sc.nextInt();
            }
            System.out.println(sum);
        }
    }
		
}

(6)A+B

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

示例1

输入
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 sc=new Scanner(System.in);
        
         while(sc.hasNext()){
            int n = sc.nextInt();
            int sum = 0;
            for(int i = 0;i<n;i++){
                
                sum+=sc.nextInt();
            }
            System.out.println(sum);
        }
    }
		
}

(7)A+B

输入描述:
输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。
输出描述:
每组数据输出求和的结果

示例1

输入
1 2 3
4 5
0 0 0 0 0
输出
6
9
0
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
 
             while(sc.hasNextLine()){
             String [] s=sc.nextLine().split(" ");
                 int sum=0;
                 for(int i=0;i<s.length;i++){
                     sum=sum+Integer.parseInt(s[i]);
                 }
                 System.out.println(sum);
         }
         }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值