杭电1042(N!)

点击打开杭电1042

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
c代码实现:

#include<stdio.h>
#include<string.h>
int main ()
{
    int t,max,n,c,i,j,a[90001];
    while (scanf("%d",&n)==1)
    {
        memset(a,0,sizeof(a));
        a[0]=1;max=1;
        for(i=1;i<=n;i++)
        {
            c=0;
            for(j=0;j<max;j++)
            {
                a[j]=a[j]*i+c;
                c=a[j]/10000;
                a[j]=a[j]%10000;
                if(c&&max<=j+1)
                max++;
            }
        }
        printf("%d",a[max-1]);
        for(j=max-2;j>=0;j--)
             printf("%04d",a[j]);
        printf("\n");
    }
    return 0;
}
java代码实现:

import java.math.BigInteger;
import java.util.Scanner;
class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			int n=sc.nextInt();
			BigInteger p=BigInteger.ONE;
			for(int i=2;i<=n;i++){
				p=p.multiply(BigInteger.valueOf(i));
			}
			System.out.println(p);
		}
	}
}
时间: 2024-10-30 03:53:35

杭电1042(N!)的相关文章

N!(杭电1042)

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 55971    Accepted Submission(s): 15886 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one

杭电1042

1 #include<stdio.h> 2 #include<string.h> 3 long long a[10000]; 4 const int maxn = 1e9; 5 int main() 6 { 7 int n,i,j,len=0; 8 while(~scanf("%d",&n)) 9 { 10 memset(a,0,sizeof a); 11 for(i=2,a[0]=1,len=1; i<=n; ++i) 12 { 13 for(j

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

一个人的旅行 HDU杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b