网易2017秋招---优雅的点

做了这么多的编程题,其实我感觉题目只要找到思路 ,代码实现是非常容易的,因为程序语言不懂的地方可以百度,

而为一个题目找到思路却是没办法搜索的,除非题目是现成可以被搜索的

这个题目求解十分容易,用O(n2)的算法 进行遍历,找到非x,y轴上的的坐标点 就+=4  在x y轴就+=2

思考花了3分钟,编码2分钟,比较入门的题

package com.net163.question;

import org.junit.Test;

/**
 * Created by Administrator on 2016/11/11 0011.
 */
public class CircleElegantPoint {
    private int number = 25;
    private int  count = 0;
    @Test
    public void countElegantPoint(){
        Integer i = Double.valueOf( Math.sqrt(number)).intValue();
        for(int j = 0 ;  j <= i ; j++){
            //System.out.println(j);
            for(int g = 0 ; g <=i; g++){
                if ((Math.pow(j,2)+Math.pow(g,2)) == number){
                    if (j==0 || g == 0){
                        count +=2;
                    }else{
                        count +=4;
                    }
                }
            }
        }
        System.out.println(count);

    }

}

 

posted on 2016-11-11 07:23  winters86  阅读(191)  评论(0编辑  收藏  举报