算法题的输入大总结
趕緊收藏吧,小白必備知識了
本文以求和為例
多組輸入,每組輸入共一行,包括兩個整數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);}} } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: 「经验分享」新鲜大红椒怎么腌好吃
- 下一篇: 梅花鹿寓意是什么