LeeBlog

导航

HDU 1042 N!

JAVA

import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.math.BigDecimal;;

public class aa
{
	public static void main( String args[] ) 
	{
		int a;
		Scanner in = new Scanner ( System.in );
		//int t = in.nextInt();
		while( in.hasNext() )
		{
			a = in.nextInt();
			BigInteger res = BigInteger.ONE;
			for( int i = 1; i <= a ; ++i )
					res = res.multiply( BigInteger.valueOf((long)i) );
			System.out.println( res );
		}
	}
}

此题是以大数相乘的基本题,直接把模板套上即可

#include<stdio.h>
#include<string.h>
const int Maxsize = 1000000;
int num[Maxsize],n,len;
void cal( int n )
{
     num[0] = 1;
     for( int i = 1; i <= n; ++i )
     {
          int j = 0,t = 0;
          while( num[len] )//没有这里直接用Maxsize会超时的 
                 ++len;
          len = len < Maxsize - 151 ? len : Maxsize;//防止len + 151 >Maxsize 
          for( ; j < len + 150; ++j )
          {
               num[j] *= i;//每一位都与i相乘 
               t += num[j];//乘完后进位 
               num[j] = t % 10;
               t /= 10;
           }
      }
 }
int main( )
{
    while( scanf( "%d",&n ) != EOF )
    {
           memset( num,0,sizeof( num ) );
           len = 0;
           cal( n );
           int j = len + 150;
           while( !num[j] )//找长度 
                  --j;
           for( ; j >= 0; --j )
                printf( "%d",num[j] );
           puts( "" );
           }
    return 0;
}

邪恶的JAVA,几句话就解决了,比赛的时候优先用JAVA

import java.math.BigInteger;
import java.util.Scanner;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner ( System.in );
		BigInteger one = BigInteger.ONE;
		while ( cin.hasNext() ) {
			BigInteger N = cin.nextBigInteger();
			BigInteger sum = new BigInteger ( "1" );
			for ( BigInteger i = BigInteger.ONE; i.compareTo(N) <= 0; i = i.add(one) ) {
				sum = sum.multiply( i );
			}
			System.out.println( sum );
		}
	}

}

posted on 2011-03-29 17:26  LeeBlog  阅读(247)  评论(0编辑  收藏  举报