Skip to content

L1-084 拯救外星人

T.jpg

你的外星人朋友不认得地球上的加减乘除符号,但是会算阶乘 —— 正整数 N 的阶乘记为 “N!”,是从 1 到 N 的连乘积。所以当他不知道“5+7”等于多少时,如果你告诉他等于“12!”,他就写出了“479001600”这个答案。

本题就请你写程序模仿外星人的行为。

输入格式:

输入在一行中给出两个正整数 A 和 B。

输出格式:

在一行中输出 (A+B) 的阶乘。题目保证 (A+B) 的值小于 12。

输入样例:

tex
3 6
3 6

输出样例:

tex
362880
362880

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 sum = a + b;
        int ans = 1;
        for (int i = 1; i <= sum; i++) {
            ans *= i;
        }
        System.out.println(ans);
    }
}
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 sum = a + b;
        int ans = 1;
        for (int i = 1; i <= sum; i++) {
            ans *= i;
        }
        System.out.println(ans);
    }
}

Released under the MIT License.