NetEase 2017 School recruit Programming: Elegant dot (php version)

Source: Internet
Author: User

The topics are as follows:

small easy to have a circle center at the origin of the coordinates, small easy to know the radius of the circle square. Xiao Yi that the dots on the circle and the horizontal ordinate are integers are elegant, small easy now looking for an algorithm to calculate the number of elegant points, please help him.
For example, if the square of the radius is
The elegant points are: (+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/-5, 0), altogether 12 points.


<?php

function Countgracepoint ($n) {

$count = 0;

for ($x = 1; $x * $x < $n; $x + +) {

$y = (int) sqrt ($n-$x * $x);

if ($x * $x + $y * $y = = $n) {

$count + +;

}

}

$count *= 4;//Because of the existence of four quadrants, there are four cases,

$x = (int) sqrt ($n);

if ($x * $x = = $n) $count + = 4;//A coordinate of 0, there are four kinds of cases

return $count;

}

Echo Countgracepoint (25);//12

?>


This article is from the "12160707" blog, please be sure to keep this source http://12170707.blog.51cto.com/12160707/1880516

NetEase 2017 School recruit Programming: Elegant dot (php version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.