Skip to content

7-19 数字宝宝

baby.png

为了教宝宝做算术,老师布置了一个作业,题目是这样的:给宝宝两个小于 1000 的正整数,要求宝宝把第一个数字的每一位加起来,得到一个数字 A;再把第二个数字的每一位乘起来,得到一个数字 B。最后要求宝宝把 A 和 B 并排放,大的那个放左边,小的放右边。

这个作业可有点难,下面就请你写个程序,帮助宝宝完成。

输入格式:

输入在一行中给出两个小于 1000 的正整数,数字间以空格分隔。

输出格式:

在一行中按老师的要求输出结果。

输入样例 1:

tex
123 45
123 45

输出样例 1:

tex
206
206

输入样例 2:

tex
67 890
67 890

输出样例 2:

tex
130
130

Solution:

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int a = Integer.parseInt(input[0]);
        int b = Integer.parseInt(input[1]);
        int A = 0, B = 1;
        while (a != 0) {
            A += a % 10;
            a /= 10;
        }
        while (b != 0) {
            B *= b % 10;
            b /= 10;
        }
        if (A >= B) {
            System.out.printf("%d%d", A, B);
        } else {
            System.out.printf("%d%d", B, A);
        }
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int a = Integer.parseInt(input[0]);
        int b = Integer.parseInt(input[1]);
        int A = 0, B = 1;
        while (a != 0) {
            A += a % 10;
            a /= 10;
        }
        while (b != 0) {
            B *= b % 10;
            b /= 10;
        }
        if (A >= B) {
            System.out.printf("%d%d", A, B);
        } else {
            System.out.printf("%d%d", B, A);
        }
    }
}

Released under the MIT License.