# ACM(java)--简单输入输出题

## 简单输入输出题

A - A+B for Input-Output Practice (I)

Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input
1 5
10 20

Sample Output
6
30

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a,b;
while(input.hasNextInt()){
a = input.nextInt();
b = input.nextInt();
System.out.println(a+b);
}
}
}
public boolean hasNextInt()

IllegalStateException - 如果此扫描器已关闭


B - A+B for Input-Output Practice (II)
Input
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input
2
1 5
10 20

Sample Output
6
30

import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c;
a = sc.nextInt();
while(a>0){
b = sc.nextInt();
c = sc.nextInt();
System.out.println(b+c);
a--;
}
}
}


C - A+B for Input-Output Practice (III)
Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input
1 5
10 20
0 0

Sample Output
6
30

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

D - A+B for Input-Output Practice (IV)
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Sample Input
4 1 2 3 4
5 1 2 3 4 5
0

Sample Output
10
15

import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,sum ,i,j;
while(sc.hasNext()){
n = sc.nextInt();
if(n==0){
break;
}
sum = 0;
for(i = 0;i < n;i++){
j = sc.nextInt();
sum = sum+j;
}
System.out.println(sum);
}
}
}


H - A+B for Input-Output Practice (VIII)
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.

Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output
10

15

6

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,sum,n,i;
n = sc.nextInt();
while(n>0){
a = sc.nextInt();
if(a==0){
break;
}
sum=0;
for(i=0;i < a;i++){
b = sc.nextInt();
sum = sum+b;
}
System.out.println(sum);
if(n!=1){
System.out.println();
}
n--;
}
}
}

I - ASCII码排序
Input

Output

Sample Input
qwe
asd
zxc

Sample Output
e q w
a d s
c x z

import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char a,b,c;
String s;
while(sc.hasNext()){
s = sc.nextLine();
char[]w= s.toCharArray();
Arrays.sort(w);
System.out.println(w[0]+" "+w[1]+" "+w[2]);
}

}

}


K - 平方和与立方和
Input

Output

Sample Input
1 3
2 5

Sample Output
4 28
20 152

x必须小于等于y

import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c,i;
long sum,sum1;
while(sc.hasNext()){
a = sc.nextInt();
b = sc.nextInt();
if(a>=b){
c = a;
a = b;
b = c;
}
sum = 0;
sum1 = 0;
for(i = a;i<=b;i++){
if(i%2==0){
sum = sum+i*i;
}else{
sum1 = sum1+i*i*i;
}
}
System.out.println(sum+" "+sum1);
}
}
}