首页 > 试题广场 >

多组_A+B_零尾模式

[编程题]多组_A+B_零尾模式
  • 热度指数:11096 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定若干组测试数据,最后一组数据为 0\ 0 ,作为输入的结尾。
每组数据有两个整数 ab ,请你求出 a+b 的值。

输入描述:
每行有两个整数 a\ (\ 0 \leq a \leq 10^9\ )b\ (\ 0 \leq b \leq 10^9\ )
最后一组数据为 0\ 0 ,作为输入的结尾。


输出描述:
输出若干行,每行一个整数,代表 a+b 的值。
示例1

输入

1 2
114 514
2024 727
0 0

输出

3
628
2751
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();

            if (a == 0 && b == 0) {
                break; 
            }

            System.out.println(a + b);
        }

        scanner.close();
    }
}

发表于 2025-03-08 16:32:42 回复(1)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
   
        while (in.hasNextInt()) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = a + b;
            //计算出一行的结果后,先进行判断,因为最后一组数据以0 0 结尾,所有不为0时可以输出
            if(!(c==0)){
                System.out.println(c);
            }else{
                //如果为0时,则进行hasNextInt()判断,true则说明这不是最后一组,所有输出结果
                if(in.hasNextInt()){
                    System.out.println(c);
                }else{
                    break;
                }
            }
        }
    }
}
发表于 2024-09-25 11:32:24 回复(0)