/*20141023 hanting*/
#include <iostream>
#include <vector>
using namespace std;
vector<int> stage(int);
vector<int> mul(vector<int>,vector<int>);
int main()
{
int n;
while(cin>>n)
{
cout<<n<<"!"<<endl;
if(!n)
{
cout<<1<<endl;
continue;
}
vector<int> output=stage(n);
for(int i=output.size()-1;i>=0;i--)
{
if(output[i]<10 && i!=output.size()-1) cout<<"00";
else if(output[i]<100 && i!=output.size()-1) cout<<"0";
cout<<output[i];
}cout<<endl;
}
return 0;
}
vector<int> stage(int num)
{
vector<int> return_num;
int Num=num;
while(num)
{
return_num.push_back(num%1000);
num/=1000;
}
if(Num==1)
{
return return_num;
}
else
{
return mul(return_num,stage(Num-1));
}
}
vector<int> mul(vector<int> num1,vector<int> num2)
{
vector<int> result(num1.size()+num2.size(),0);
for(int i=0;i<num1.size();i++)
{
for(int j=0;j<num2.size();j++)
{
result[i+j]+=num1[i]*num2[j];
}
}
for(int i=1;i<result.size();i++)
{
result[i]+=result[i-1]/1000;
result[i-1]%=1000;
}
for(int i=result.size()-1;i>=0;i--)
{
if(!result[i]) result.pop_back();
else break;
}
return result;
}
----------
/* 20151027
* hanting
* UVa 623 - 500!
* C++
*/
#include <iostream>
#include <iomanip> // setw, setfill
using namespace std;
const int maxn = 700;//每一格存4位數
class BigInteger
{
private:
int val[maxn];
int digit;
public:
BigInteger():val{0},digit(0){}
friend ostream& operator << (ostream &out, BigInteger B);
BigInteger operator * (int n);
void operator = (int n);
};
BigInteger fact[1005];
int main()
{
fact[0] = 1;
fact[1] = 1;
for(int i = 2; i < 1005; i++)
{
fact[i] = fact[i-1] * i;
}
int num;
while(cin >> num)
{
cout << num << "!" << endl << fact[num] << endl;
}
return 0;
}
ostream& operator << (ostream &out, BigInteger B)
{
out << B.val[B.digit-1] ;
for(int i = B.digit - 2; i >= 0; i-- )
{
out << setw(4) << setfill('0') << B.val[i];
}
return out;
}
BigInteger BigInteger::operator * (int n)
{
BigInteger result;
for(int i = 0; i < digit; i++)
{
result.val[i] += val[i] * n;
result.val[i+1] += result.val[i] / 10000;
result.val[i] %= 10000;
}
result.digit = this ->digit + 10;
while(!result.val[result.digit-1]) result.digit--;
return result;
}
void BigInteger::operator = (int n)
{
while(n)
{
val[digit++] = n%10000;
n /= 10000;
}
}
2014年10月23日 星期四
[UVA] 623 - 500!
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言