zoukankan      html  css  js  c++  java
  • POJ 1423 Big Number

    Big Number

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1
    Problem Description
    In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
     
    Input
    Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 <= m <= 10^7 on each line.
     
    Output
    The output contains the number of digits in the factorial of the integers appearing in the input.
     
    Sample Input
    2 10 20
     
    Sample Output
    7 19
     
    Source
    PKU
     
     

    给一个数n,求n的阶乘的位数;

    直接高精度计算显然很复杂,换个思路,对于一个数m,m的位数就是(int)log10(m)+1。如果m=n*(n-1)*(n-2)*......*1,那么log10(m)=log10(n)+

    log10(n-1)+.....+log10(2)+log10(1)。即m的位数是( int )( log10(n)+log10(n-1)+.....+log10(2)+log10(1) )+1。由于题目要求的m很大,独立计算会造成重复计算某些值,所以采用预处理出,所有的结果。

    //先将所以要求的数都存起来,由于最后要按输入顺序输出结果,所以还要存入id
    //按值从小到大顺序排列,先计算最小值的对数,后面的数都建立在他的基础上,
    //和上述代码思路相同。最后按id从小到大排列输出结果即可,这样可以避免对内存的消耗

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    struct node{
        int id,num;
        double result;
    }a[1010];
    
    int flag;
    
    int cmp(node c,node d){
        if(flag)
            return c.num<d.num;
        return c.id<d.id;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        while(~scanf("%d",&n)){
            for(int i=0;i<n;i++){
                scanf("%d",&a[i].num);
                a[i].id=i;
                a[i].result=0;
            }
            flag=1;
            sort(a,a+n,cmp);
            for(int i=1;i<=a[0].num;i++)
                a[0].result+=log10(i);
            for(int i=1;i<n;i++){
                a[i].result=a[i-1].result;
                for(int j=a[i-1].num+1;j<=a[i].num;j++)
                    a[i].result+=log10(j);
            }
            flag=0;
            sort(a,a+n,cmp);
            for(int i=0;i<n;i++)
                printf("%d\n",(int)a[i].result+1);
        }
        return 0;
    }
  • 相关阅读:
    angular.js 头部默认值,不使用json提交数据
    D1-FFmpeg拼接视频
    B23-Carthage的使用
    B22-SVN在iOS开发的使用中遇到的问题
    C4-Cordova在iOS平台的使用
    C2-PhoneGap的环境搭建及iOS项目创建
    C0-PhoneGap之移动开发策略的选择(翻译)
    C1-PhoneGap和Cordova的关系和认识
    B21-iOS 开发的一些tips(下)
    B17-禅与 Objective-C 编程艺术的阅读笔记
  • 原文地址:https://www.cnblogs.com/jackge/p/3048395.html
Copyright © 2011-2022 走看看