N!水题
//题目是求N!的问题,思路:设定一个整形数组来存放每次计算过后的值
有两个for循环,第一个for循环每次加进一个数
然后在第二个for循环里面计算出此时的阶乘,比如9999,先给出i=2
在第二个for循环里面计算出2的阶乘,保存在数组里,然后i=3,在计算3的阶乘,后面以此类推。。。
#include<stdio.h>
#include <iostream>
#include<string.h>
#include<stdlib.h>
#define N 8000 //只需这么多就足够了!!!
using namespace std;
int s[N];
int main()
{
int i,j,k,t,n;
while(scanf("%d",&n)!=EOF)
{
memset(s,0,sizeof(s)); //每次使用前要清零
s[0]=1;
for(i=2;i<=n;++i) //计算n!
{ for(t=0,j=0;j<N;++j)//N也不算大,不用再改进了,要改进的话就是去计算n!的位数,计算方法如上
{
k=s[j]*i+t; //看到K了吗,这就是我们上面我们为什么只是10^5的原因,因为k就算是无符号型最大为 2^32-1,刚刚超过10^9,而s[j]<10^5,i<10^4,t<10^4,加一块最大值小于2^31-1,假如让s元素一个存6位的话,s[j]<10^6,i<10^4,t<10^4,就可能超过2^32-1了!所以s数组元素最多存5位!!
s[j]=k%100000;
t=k/100000;
//cout<<k<<" "<<s[j]<<" "<<t<<endl;
}
cout<<s[0]<<" "<<s[1]<<endl;//自己测试的,如果是9的话,刚好s[0]满5位了,然后数的最高位存放到s[1]里面。。。
}
for(i=N-1;!s[i];i--); //除去前导零
printf("%d",s[i]);//第一个元素不要求输出多余零
while(i)
printf("%05d",s[--i]);//这些需要输出前面多余的零,这个你懂的,否则一直WA!!
putchar('\n');
}
system("pause");
return 0;
}
N!水题的更多相关文章
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,
1195: 相信我这是水题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 821 Solved: 219 Description GDUT中有个风云人 ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
- 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题
B - 大还是小? Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Description 输入两个实数,判断第一个数大 ...
- ACM水题
ACM小白...非常费劲儿的学习中,我觉得目前我能做出来的都可以划分在水题的范围中...不断做,不断总结,随时更新 POJ: 1004 Financial Management 求平均值 杭电OJ: ...
- CF451C Predict Outcome of the Game 水题
Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
随机推荐
- Linux小知识点汇总
1.crontab (1)crontab每10秒执行一次 * * * * * /bin/date >>/tmp/date.txt * * * * * sleep 10; ...
- Android学习----五大布局
1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...
- js循环便利json数据
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name: ...
- span宽度高度设置
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ASP.NET程序从IIS6移植到IIS7时出现500.22错误
最可能的原因: • 此应用程序在 system.web/httpModules 节中定义配置. 可尝试的操作: • 将配置迁移到 system.webServer/modules 节 ...
- UIImageView设置为圆形
CGFloat headimageX = self.view.frame.size.width * 0.2; CGFloat headimageY = self.view.frame.size. ...
- LayerDrawable 资源
与 StateListDrawable 有点类似,LayerDrawable 也可包含一个 Drawable 数组,因此系统 将会按这些 Drawable 对象的数组顺序来绘制它们,索引最大的 Dra ...
- Open vSwitch源码阅读【转】及自己的理解【稍后更新】
http://blog.chinaunix.net/uid-24485075-id-2240075.html
- BeautifulSoup解析非标准HTML的问题
发现问题: BeautifulSoup版本:4.3.2 在用BeautifulSoup.find_all()搜索HTML时,遇到下面的代码: <a href="/shipin/dong ...
- 前端工程师和web工程师的差异
摘自园内一篇文章关于web工程师的思考,比较认同其中的一些观点 前端工程师知识结构: 精通: xhtml,css,JavaScript 熟悉:一种后端程序语言( ...