zoukankan      html  css  js  c++  java
  • HDU——1042N!(大数阶乘乘法)

    N!

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 70861    Accepted Submission(s): 20321


    Problem Description
    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
     


     

    Input
    One N in one line, process to the end of file.
     


     

    Output
    For each N, output N! in one line.
     


     

    Sample Input
    1 2 3
     


     

    Sample Output
    1 2 6

    求几十位数的阶乘,用数组保存每四位的数字,比如1320 0456 789,会变成list[0]=132,list[1]=456,list[2]=789;输出除了开头的那一组,其余用%04d(不足四位补0)。话说我不看大牛的代码也就只知道输出方式- -|||就当学习了。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<set>
    #include<map>
    #include<sstream>
    #include<algorithm>
    #include<cmath>
    #include<cstdlib>
    using namespace std;
    int list[40050]={1};//开始是1
    int len;//总长度
    void jiecheng(int a)
    {
    	for (int i=0; i<=len; i++)
    	{
    		list[i]*=a;
    		if(list[i-1]>9999&&i>0)//若超过四位数
    		{
    			list[i]+=list[i-1]/10000;//先进一位
    			list[i-1]%=10000;//然后前一位取模					
    		}
    		if(list[len]>=10000)
    			len++;
    	}	
    }
    int main (void)
    {
    	int n;
    	while (cin>>n)
    	{
    		memset(list,0,sizeof(list));//每次都清空一下,防止干扰
    		list[0]=1;
    		len=0;
    		for (int i=1; i<=n; i++)
    		{
    			jiecheng(i);
    		}
    		printf("%d",list[len]);//开头的一组正常输出
    		for (int i=len-1; i>=0; i--)
    		{
    			printf("%04d",list[i]);//不足四位补0
    		}
    		cout<<endl;
    	}
    	return 0;
    }
  • 相关阅读:
    小学生学python(六)类与函数
    Windows 10 搭建 Flask
    CentOS 8 上安装 python3
    6_7 selenium使用代理IP
    6_6 模拟浏览器的前进后退&窗口句柄切换
    6_5 selenium操作cookie
    6_4 行为链
    6_3 selenium操作表单元素
    6_2 selenium定位元素的方法
    6_1 selenium 安装与 chromedriver安装
  • 原文地址:https://www.cnblogs.com/Blackops/p/5356408.html
Copyright © 2011-2022 走看看