美国面试题10
1 .题目:
It is a fact that there exist two numbers x and y such that x! + y! = 10!. Write a method named solve10 that returns the values x and y in an array.
The notation n! is called n factorial and is equal to n * n-1 * n-2 * … 2 * 1, e.g., 5! = 5*4*3*2*1 = 120.
If you are programming in Java or C#, the function prototype is
int[ ] solve10() where the length of the returned array is 2.
If you are programming in C++ or C, the function prototype is
int * solve10() where the length of the returned array is 2.
Please be sure that the method solve10 returns an array, a, with two elements where a[0] == x, a[1] == y and x! + y! = 10!.
2. java代码样例:
public class solve10 {
public static void main(String[] args) {
// System.out.println(nfactorial(0));
System.out.println(solve());
}
public static int solve(){
int x=0,y=0;
int sum = nfactorial(10);
for(int i=10;i>=0; i--){
x=nfactorial(i);
for (int j=9;j>=0;j-- ){
y=nfactorial(j);
// System.out.println("y="+y);
if(x+y==sum){
return 2;
}
}
}
return 0;
}
public static int nfactorial(int a){
if(a==0){
return 0;
}
int sum = 1;
for(int i = a; i>0; i--){
sum = sum*i;
}
return sum;
}
}