设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 大数据 > 正文

nyoj28 大数阶乘

发布时间:2021-01-18 14:46 所属栏目:125 来源:网络整理
导读:nyoj24 大数阶乘 链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28 正确代码: #includestdio.h#includestring.h#includestdlib.h#includemath.h#includealgorithmusing namespace std;const int maxn=40000;int a[maxn]={0};int main(){ int m,i

nyoj24 大数阶乘
链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28
 
正确代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=40000;

int a[maxn]={0};
int main()
{
  int m,i,j;
  scanf("%d",&m);
  a[0]=1;
  for(i=2;i<=m;i++)
  {
    int carry=0;
    for(j=0;j<maxn;j++)
    {
      int res=a[j]*i+carry;
      a[j]=res%10;
      carry=res/10;
    }
  }
  for(j=maxn-1;j>=0;j--)
    if(a[j])
      break;
  for(i=j;i>=0;i--)
    printf("%d",a[i]);
    printf("\n");
  return 0;
}
        
超时代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;

char s[100000];
int main()
{
  int m,&m);
  strcpy(s,"1");
  for(i=2;i<=m;i++)
  {
    int carry=0;
    for(j=0;j<strlen(s);j++)
    {
      int res=(s[j]-'0')*i+carry;
      carry=res/10;
      s[j]=(res%10)+'0';
    }
    while(carry!=0)
    {
      s[j++]=(carry%10)+'0';
      carry/=10;
    }
  }
  reverse(s,s+strlen(s));
  printf("%s\n",s);
  return 0;
}
      
      
      
        

 

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读