USACO-Section3.2 Factorials【模拟法】
2018-02-13 17:20
411 查看
题目描述:
阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了。 你的任务是找到阶乘最前面的非零位。举个例子:5!=1*2*3*4*5=120,所以5!的最靠后的非零位是2。
7!=1*2*3*4*5*6*7=5040,所以最靠后的非零位是4。
INPUT FORMAT:
共一行,一个不大于4,220的正整数NOUTPUT FORMAT:
共一行,输出N!最靠后的非零位。SAMPLE INPUT
7
SAMPLE OUTPUT
4
解题思路:
判断乘数中可以分出来的5的个数即可。#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> using namespace std; int N; int main(){ FILE *fin = fopen ("fact4.in", "r"); FILE *fout = fopen ("fact4.out", "w"); fscanf(fin,"%d\n",&N); int ans=1; for(int i=1;i<=N;i++){ int temp=i; while(temp%10==0){//乘数末尾的0直接去掉 temp=temp/10; } while(temp%5==0){//乘数能被5整除结果就除以2,注意末尾为6和2时需要加10计算,因为不可能结果为奇 if(ans==6||ans==2) ans=ans+10; ans=ans/2; temp=temp/5; } if(temp>0)//如果temp为5的整数倍就跳过 ans*=temp%10; ans=ans%10;//只保留一位结果 } fprintf(fout,"%d\n",ans); exit(0); }
相关文章推荐
- USACO Section 3.2 Factorials (fact4)
- USACO-Section 3.2 Factorials(模拟)
- USACO Section 3.2 Factorials
- USACO section 3.2 Factorials(dp)
- USACO section 3.2 Factorials(dp)
- usaco-Section 3.2-Factorials
- USACO Section 3.2 Factorials(n!求最右边非零数)
- USACO Section 3.2 Factorials - 高精度模拟~
- USACO-Section3.2 Spinning Wheels【模拟法】
- USACO Section 3.2: Factorials
- USACO Section 3.2 Stringsobits - 排列组合
- [USACO3.2]阶乘 Factorials
- USACO section 3.2 Feed Ratios(高斯定理)
- USACO-Section3.2 Sweet Butter【Dijkstra算法】
- usaco-Section 3.2-Stringsobits
- USACO-Section 3.2 Spinning Wheels(模拟)
- USACO Section 3.2: Feed Ratios
- 【USACO 3.2】Factorials(阶层非零尾数)
- USACO-Section 3.2 Sweet Butter(最短路[Dijkstra])
- USACO section 3.2 Magic Squares(STL+广搜)