zoukankan      html  css  js  c++  java
  • nyoj 65-另一种阶乘问题 (Java 高精度)

    65-另一种阶乘问题


    内存限制:64MB 时间限制:3000ms 特判: No
    通过数:16 提交数:18 难度:1

    题目描述:


    大家都知道阶乘这个概念,举个简单的例子:5!=1*2*3*4*5.现在我们引入一种新的阶乘概念,将原来的每个数相乘变为i不大于n的所有奇数相乘例如:5!!=1*3*5.现在明白现在这种阶乘的意思了吧!

    现在你的任务是求出1!!+2!!......+n!!的正确值(n<=20)

    输入描述:

    第一行输入一个a(a<=20),代表共有a组测试数据
    接下来a行各行输入一个n.
    

    输出描述:

    各行输出结果一个整数R表示1!!+2!!......+n!!的正确值

    样例输入:

    2
    3
    5

    样例输出:

    5
    23

    分析:
      1、阶乘公式的变形,因为数据比较小不用高精度也可以解决问题

    核心代码:
     1 int a = scan.nextInt();
     2 ans = BigInteger.ZERO;
     3 for(int i = 1; i <= a; ++ i) {
     4     temp = BigInteger.ONE;
     5     for(int j = 1; j <= i; j += 2) {
     6         temp = temp.multiply(BigInteger.valueOd(j));
     7     }
     8     ans = ans.add(temp);
     9 }
    10 Systemp.out.println(ans);

    java代码实现(AC):

     1 import java.util.*;
     2 import java.io.*;
     3 import java.math.*;
     4 
     5 public class Main {
     6     public static void main(String args[]) {
     7         Scanner scan = new Scanner(System.in);
     8         BigInteger ans ,temp;
     9         int t = scan.nextInt();
    10         while (t > 0) {
    11             t--;
    12             int a = scan.nextInt();
    13             ans = BigInteger.ZERO;
    14             for(int i = 1; i <= a; ++ i){
    15                 temp = BigInteger.ONE;
    16                 for(int j = 1; j <= i; j += 2){
    17                     temp = temp.multiply(BigInteger.valueOf(j));
    18                 }
    19                 ans = ans.add(temp);
    20             }
    21             System.out.println(ans);
    22         }
    23     }
    24 }
  • 相关阅读:
    BNU Online Judge-29140
    HDU-1022-Train Problem I
    HDU-1312-Red and Black
    BNU Online Judge-34978-汉诺塔
    BNU Online Judge-34976-数细菌
    BNU Online Judge-34973-Liserious战队
    HDU-1010-Tempter of the Bone
    HDU-1518-Square
    thinkphp笔记
    1210. 连号区间数
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9110659.html
Copyright © 2011-2022 走看看