题目是这样的
Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vičkopolis. He almost even fell into a depression from boredom!
Leha came up with a task for himself to relax a little. He chooses two integers Aand B and then calculates the greatest common divisor of integers "A factorial" and "B factorial". Formally the hacker wants to find out GCD(A!, B!). It's well known that the factorial of an integer x is a product of all positive integers less than or equal to x. Thus x! = 1·2·3·...·(x - 1)·x. For example 4! = 1·2·3·4 = 24. Recall that GCD(x, y) is the largest positive integer q that divides (without a remainder) both x and y.
Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren't you?
Input
The first and single line contains two integers A and B (1 ≤ A, B ≤ 109, min(A, B) ≤ 12).
Output
Print a single integer denoting the greatest common divisor of integers A! and B!.
Example
4 3
6
Note
Consider the sample.
4! = 1·2·3·4 = 24. 3! = 1·2·3 = 6. The greatest common divisor of integers 24 and 6is exactly 6
看到题目的第一眼我觉得这题目思路清晰 不难
我首先的源代码是
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int jc(int a0);
int gys(int a1,int b1);
int a,b;
while(cin>>a>>b){
cout<<gys(jc(a),jc(b));}
}
int jc(int a0)/*按题目思路先求阶乘*/
{
int k;
if(a0==1)k=a0;
else k=a0*jc(a0-1);
return k;
}
int gys(int a1,int b1)/*然后求最大公约数*/
{
int q;
for(int k=1;k<=min(a1,b1);k++)
{
if(a1%k==0&&b1%k==0)q=k;
}
return q;
}
可是在错了四次之后 我就觉得不对劲了
经过理性思考以后终于发现了原因:如果我按首先的源码的话,当输入比较大的话(例如1000)就会超出内存,所以就只能从求阶乘入手了,题意是要我们求a!,b!的最大公约数c(a>b),a!,b!存在关系a!/b!=a*(a-1)*(a-2).......*b所以a!与b!的最大公约数其实就是b!,所以我们其实只要求输入中较小数b的阶乘b!,这是改进后的源代码
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a,b,k=1;
cin>>a>>b;
for(int i=1;i<=min(a,b);i++)
{
k*=i;
}
cout<<k<<endl;
}
是不是简单了很多,以后看题目还是得先多多思考,不然会浪费好多时间(ง •_•)ง!!!