zoukankan      html  css  js  c++  java
  • 题目1179:阶乘

    题目描述:

    输入n,
    求y1=1!+3!+...m!(m是小于等于n的最大奇数)
    y2=2!+4!+...p!(p是小于等于n的最大偶数)。

    输入:

    每组输入包括1个整数:n

    输出:

    可能有多组测试数据,对于每组数据,
    输出题目要求的y1和y2

    样例输入:
    4
    样例输出:
    7 26
     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3  
     4 public class Main{
     5     public static void main(String[]args){
     6     Scanner in=new Scanner(System.in);
     7     long[]re=new long[10001];
     8     for(int i=1;i<re.length;i++)
     9         re[i]=1;
    10     for(int i=1;i<re.length;i++){
    11         for(int j=1;j<=i;j++){
    12         re[i]*=j;
    13         }
    14     }
    15     while(in.hasNext()){
    16         int n=in.nextInt();
    17         BigInteger y1=new BigInteger("0");
    18         BigInteger y2=new BigInteger("0");
    19         for(int i=1;i<=n;i++){
    20         BigInteger an=JC(i);
    21         if(i%2!=0){
    22             y1=y1.add(an);
    23         }
    24         else{
    25             y2=y2.add(an);
    26         }
    27         }
    28         System.out.println(y1+" "+y2);
    29     }
    30 }
    31     public static BigInteger JC(int n){
    32     BigInteger an=new BigInteger("1");
    33     for(int i=1;i<=n;i++){
    34         BigInteger tem=new BigInteger(String.valueOf(i));
    35         an=an.multiply(tem);
    36     }
    37     return an;
    38     }
    39  }
    40  
    41 /**************************************************************
    42     Problem: 1179
    43     User: 0000H
    44     Language: Java
    45     Result: Accepted
    46     Time:290 ms
    47     Memory:17448 kb
    48 ****************************************************************/
  • 相关阅读:
    3.node.js AssertionError: false == true错误解决
    9.获取当前时区时间和utc时间的工具方法
    2.Express封装mysq方法
    1.Express初识
    poj 3617
    前缀和
    pop 反序列化
    Reverse前两个题
    前两个Web题
    Misc
  • 原文地址:https://www.cnblogs.com/qq1029579233/p/4470647.html
Copyright © 2011-2022 走看看