江湖救急,就一道题

新手上路,请多包涵

题目:输入任意整数,当N为奇数时,计算n(n-2)(n-4)......(n-n+1),当N为偶数时,计算n(n-2)(n-4)......(n-n+2)

希望java语言完成,效率尽可能高。输入整数N(正整数 负整数),输出结果 和 运行时间ms

谢谢各位大神帮助,能给个思路也行

阅读 1.3k
1 个回答
public class App {
    public static void main(String[] args) throws Exception {
        try (Scanner sc = new Scanner(System.in);) {
            String inputText = sc.nextLine();
            int num = Integer.parseInt(inputText);
            long result = 1;
            int start = num % 2 == 0 ? 2 : 1;
            for (int i = start; i <= num; i += 2) {
                result *= i;
            }
            System.out.println("result is " + result);
        } catch (Exception e) {
            // ...
        }
    }
}
推荐问题