算法题的输入大总结

赶紧收藏吧,小白必备知识了

本文以求和为例

多组输入,每组输入共一行,包括两个整数A, B

Sample Input
1 2
12 24
400 500
Sample Output
3
36
900
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	while(sc.hasNext()) {
    		System.out.println(sc.nextInt()+sc.nextInt());
    	}
    }
}

第一行是数据的组数N,从第二行开始是N组由两个整数(A和B)构成的数据,A和B之间用空格隔开,每组输入单独占一行

Sample Input
2
1 2
10 20
Sample Output
3
30
//2
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n=sc.nextInt();
    	while(n-->0) {
    		System.out.println(sc.nextInt()+sc.nextInt());
    	}
    }
}

多组数据:每组由两个整数(A和B)构成,A和B之间用空格隔开,每组输入单独占一行。当输入为"0 0"时,输入结束。"0 0"这组数据不处理。

Sample Input
1 2
3 4
10 20
0 0
Sample Output
3
7
30
//3
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	while(true) {
    		int a=sc.nextInt();
    		int b=sc.nextInt();
    		if(a==0 && b==0)break;
    		System.out.println(a+b);
    	}
    }
}

输入包含多个测试用例。每个测试用例包含一个正整数N,随后是N个整数跟在同一行上。当某个测试用例以0开始,终止输入,且该用例不处理。

Sample Input
3 1 2 4
1 23
5 1 3 5 7 9
0
Sample Output
7
23
25
//4
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	while(true) {
    		int a=sc.nextInt();
    		if(a==0)break;
    		int ac=0;
    		while(a-->0)ac+=sc.nextInt();
    		System.out.println(ac);
    	}
    }
}

第一行为N,下面紧跟N行数据。每行数据:开头为M,后面紧跟M个数。

Sample Input
2
1 1
2 3 4
Sample Output
1
7
//5
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n=sc.nextInt();
    	while(n-->0) {
    		int a=sc.nextInt();
    		if(a==0)break;
    		int ac=0;
    		while(a-->0)ac+=sc.nextInt();
    		System.out.println(ac);
    	}
    }
}
评论 49
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

兔老大RabbitMQ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值