您的位置 首页 编程语言

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.

美国面试题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;
    }
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。