Fork me on GitHub

Uva 568 - Just the Facts

  Just the Facts 

The expression N!, read as ``N factorial," denotes the product of the first N positive integers, where N is nonnegative. So, for example,

 

N N!
0 1
1 1
2 2
3 6
4 24
5 120
10 3628800

For this problem, you are to write a program that can compute the last non-zero digit of any factorial for ($0 \le N \le 10000$). For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce ``2" because 5! = 120, and 2 is the last nonzero digit of 120.

 

Input 

Input to the program is a series of nonnegative integers not exceeding 10000, each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute the last nonzero digit of N!.

 

Output 

For each integer input, the program should print exactly one line of output. Each line of output should contain the value N, right-justified in columns 1 through 5 with leading blanks, not leading zeroes. Columns 6 - 9 must contain `` -> " (space hyphen greater space). Column 10 must contain the single last non-zero digit of N!.

 

Sample Input 

1
2
26
125
3125
9999

 

Sample Output 

    1 -> 1
    2 -> 2
   26 -> 4
  125 -> 8
 3125 -> 2
 9999 -> 8

 

 


Miguel A. Revilla 
1998-03-10
 
复制代码
#include<stdio.h>

int main()
{

    long long i, n, m, temp;
    while(scanf("%lld", &n) != EOF)
    {
        for(m=i=1; i<=n; ++i)
        {
            temp = i;
            while(temp%10 == 0) temp /= 10;
            m *= temp;
            while(m%10 == 0) m /= 10;
            m = m%100000;
        }
        printf("%5lld -> %lld\n", n, m%10);
    }
    return 0;
}
复制代码

解题思路:

题目意思是说要你求n(n<=10000)的阶乘最后一位不为零的数字,

解决的方法主要是用求模保存在范围内的数,因为n<=10000,存储不了那么大的数,而每次要保存的数并不能仅仅是最后一位,因为可能跟下一个相乘后此时会成为零,同时为了保证不溢出,也要对第二个因子取整

差点WA了,还在测试数据还过得去,用int导致了中间的值溢出,导致最后求的不为零尾数改变,算是一题简单题了。

posted @   Gifur  阅读(688)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
TOP
点击右上角即可分享
微信分享提示