1年前 (2018-08-15)  编程语言 |   抢沙发  21 
文章评分 0 次,平均分 0.0

A number n is vesuvian if it is the sum of two different pairs of squares. For example, 50 is vesuvian because 50 == 25 + 25 and 1 + 49. The numbers 65 (1+64, 16+49) and 85 (4+81, 36+49) are also vesuvian. 789 of the first 10,000 integers are vesuvian.
Write a function named isVesuvian that returns 1 if its parameter is a vesuvian number, otherwise it returns 0. Hint: be sure to verify that your function detects that 50 is a vesuvian number!

 

 

java代码实现:

 

package com.zzy;

/**
 * 更多请关注: http://hadoopall.com
 * Created by hadoopall on 15/08/2018.
 */
public class Vesuvian {

    public static void main(String[] args) {

        System.out.println(isVesuvian(85));

        int count =0;
        for (int i = 0; i < 10000; i++) {

            if(isVesuvian(i)==1){
                count++;
            }
        }
        System.out.println("count="+count);

    }

    public static int  isVesuvian(int n){

        int max = (int) Math.sqrt(n);
        int count = 0;
        int tmp = 0;

        for (int i = 1; i < max; i++) {

            for (int j =  2; j <= max; j++) {
                if((i*i + j*j) == n && j != tmp){
                    tmp = i ;
//                    System.out.println("i="+i+",j="+j+",max="+max);
                    count ++;
                }

            }


        }



        if (count == 2){
            return 1;
        }
        return 0;
    }
}
 

除特别注明外,本站所有文章均为HadoopAll原创,转载请注明出处来自http://hadoopall.com/624.html

发表评论

表情 格式

暂无评论

登录

忘记密码 ?

切换登录

注册

扫一扫二维码分享