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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sakurakider/article/details/79185911

简单输入输出题

寒假要好好学习,天天向上,然后就用java刷下题哈,,,唉,顺便巩固下java语法练练手,毕竟要靠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
思路:这道题真是的acm的编程入门题啊啊啊,当年用C刷的时候,都栽在上面了,用java刷的时候第一次还是没过,这道题的关键是让你看清可以多次输入

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()
如果通过使用 nextInt() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 int 值,则返回 true。扫描器不执行任何输入。 
返回:
当且仅当此扫描器的下一个标记是有效的 int 值时才返回 true 
抛出: 
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
思路:这个,,就是套路一样的,题目要求是可以一直输入,直到输入到0 0 为止,结合第一题的就好了

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
思路:这道题是什么那道的加强版的题,就是每次输入一行数据,第一个数据代表这一行需要继续输入的个数,然后把他们加起来,,如果每一行第一个输入0就截至了

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
思路:这个就终于有中文题了啊,就是输入3个字符,然后按ascall排序
先输入一个字符串,然后变成字符数组,然后排序,然后输出

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
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。

Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。

Sample Input
1 3
2 5

Sample Output
4 28
20 152
思路:题意蛮简单的,就是要考虑清楚你输入的x 和 y的值的大小比较
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);
        }
    }
}

总结

第一周的题,,好多都没往上面写,就是有的写过了的就不放在上面了
算法还是要好好练啊

展开阅读全文

没有更多推荐了,返回首页