USACO / Factorials (简单模拟)
来源:互联网 发布:xboxone有线网络设置 编辑:程序博客网 时间:2024/06/05 13:24
USACO/Factorials
Factorials阶乘
N的阶乘写作N!,表示小于等于N的所有正整数的乘积。 阶乘会变大得很快,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了。 你的任务是找到阶乘最后面的非零位。举个例子:[编辑]描述
5!=1*2*3*4*5=120,所以5!的最后面的非零位是2。7!=1*2*3*4*5*6*7=5040,所以最后面的非零位是4。
格式
PROGRAM NAME: fact4
INPUT FORMAT:
(fact4.in)
共一行,一个不大于4,220的正整数N
OUTPUT FORMAT:
(fact4.out)
共一行,输出N!最后面的非零位。
SAMPLE INPUT
7
SAMPLE OUTPUT
4
分析:
这道题太简单了= =……但是为什么还要写出来呢?显然是我SB的写错了。。。。。。
刚开始就是直接在每个阶段只留下最后一个不是零的数来去乘接下来的数,但是在WA+N次debug后终于发现,只留下一个数是不够的。。。因为也许这个数和下一个数乘起来末尾就成了零而不再适合了,比如本来是24,留下一个4,接下来是25,按这种方法做4*25,结果是100,最后一个非零为1,而实际上24*25=600,最后一个非零位是6。那么回到理论中,就是决定前面数字大小的数在上一阶段只留下最后一位数时被略去了而导致结果可能出错。。。所以。。。。。。一个贪心算法油然而生(= =这个词好诡异的用法。。。):不只留1位,尽可能多留~考虑到结果为int且n最大为5000,所以我们选择保留5位。。。
代码:
/*ID:138_3531LANG:C++TASK:fact4*/#include <fstream>#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <iomanip>#include <climits>#include <vector>#include <set>#include <map>#include <algorithm>#include <string>#include <cstring>using namespace std;int MAX(int a,int b){ return a>b?a:b;}int MIN(int a,int b){ return a>b?b:a;}ifstream fin("fact4.in");ofstream fout("fact4.out");int f(int n){ int k; if (n==1) return 1; k=f(n-1)*n; while (k%10==0) k/=10; return k%10000;}int main(){ int n; fin>>n; fout<<f(n)%10<<endl;x return 0;}
- USACO / Factorials (简单模拟)
- USACO-Section 3.2 Factorials(模拟)
- 【模拟】【USACO】Factorials
- USACO Section 3.2 Factorials - 高精度模拟~
- USACO-Factorials
- USACO:Factorials
- usaco Factorials
- USACO 3.2 Factorials
- USACO 3.2.1 Factorials
- USACO 3.2.1 Factorials
- USACO Section 3.2 Factorials
- USACO 3.2 Factorials (fact4)
- USACO--3.1Factorials
- USACO-Factorials(阶乘最后一个非0数)
- USACO Factorials,DP,因子分解
- USACO section 3.2 Factorials(dp)
- USACO-Section 3.2-PROB Factorials
- USACO 3.2 Factorials阶乘(高精度)
- informix
- Number Sequence
- c语言中位段
- php冒泡排序
- 双目视图笔记
- USACO / Factorials (简单模拟)
- 最少拦截系统
- 穷人和富人的差异并不仅仅在于钱多少
- Memcache协议中文版
- 多线程分页算法
- effective java(通用程序设计)
- php二分查找
- PCI总线
- 快速排序(递归)