zoukankan      html  css  js  c++  java
  • *科学技术大学第忘记叫啥杯了

    1594: Factorials

    Time Limit: 1 Sec Memory Limit: 128 MB

    Submit: 207 Solved: 72

    [Submit][Status][Web Board]

    Description

    The factorial of an integer N, written N!, is the product of all the integers from 1 through N inclusive. The factorial quickly becomes very large: 13! is too large to store in a 32-bit integer on most computers, and 70! is too large for most floating-point variables. Your task is to find the rightmost non-zero digit of n!. For example, 5! = 1 * 2 * 3 * 4 * 5 = 120, so the rightmost non-zero digit of 5! is 2. Likewise, 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040, so the rightmost non-zero digit of 7! is 4.

    Input

    This problem includes multiple cases. The first line of input is a integer T represents the number of cases.

    For each case, there is a single positive integer N no larger than 4,220.

    Output

    For each case, output corresponding results separately.

    The output of each case is a single line containing but a single digit: the right most non-zero digit of N!.

    Sample Input

    1

    7

    Sample Output

    4​

    #include<stdio.h>

    #include<string.h>

    #include<iostream>

    #include<math.h>

    #include<algorithm>

    using namespace std;

    int a[100000];

    int main()

    {

    int t,n;

    scanf("%d",&t);

    while(t--)

    {

    scanf("%d",&n);

    int i,j,k,m;

    long long g,t;

    double s;

    s=0;

    for(k=2;k<=n;k++)

    s+=log10(k);

    m=(int)s+1;//对数累加,至于为啥我也不清楚,感觉挺实用的,求阶乘的位数

    for(k=1;k<=m;k++)

    {

    a[k]=0;

    }

    a[1]=1;

    g=0;

    for(k=2;k<=n;k++)

    {

    for(j=1;j<=m;j++)

    {

    t=a[j]*k+g;//数组累乘并进位

    a[j]=t;

    g=t/10;

    }

    }

    for(int i=1;i<=m;i++)

    {

    if(a[i]!=0)

    {

    printf("%d ",a[i]);

    break;

    }

    }

    }

    return 0;

    }

                                                                   
  • 相关阅读:
    vs 2005 使用 UpdatePanel 配置
    gridview checkbox 列
    csv 格式文件 导入导出
    UML中数据流图,用例图,类图,对象图,角色图,活动图,序列图详细讲述保存供参考
    c# 根据经纬度 求两点之间的距离
    c# 加密汇总
    日期获取 第一天,最后一天
    求点到直线的垂足
    c# 修改注册表
    HDOJ_1548 上楼梯 DJ
  • 原文地址:https://www.cnblogs.com/NaCl/p/9580224.html
Copyright © 2011-2022 走看看