优雅的点
来源:互联网 发布:win7无法打开软件 编辑:程序博客网 时间:2024/05/22 13:20
Question
小易有一个圆心在坐标原点的圆,小易知道圆的半径的平方。小易认为在圆上的点而且横纵坐标都是整数的点是优雅的,小易现在想寻找一个算法计算出优雅的点的个数,请你来帮帮他。
例如:半径的平方如果为25
优雅的点就有:(+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/-5, 0),一共12个点。
输入描述:
输入为一个整数,即为圆半径的平方,范围在32位int范围内。
输出描述:
输出为一个整数,即为优雅的点的个数
Algorithm
O(n^2)的复杂度会超时,有O(n)的解法。sqrt()函数的计算值是整型,比如sqrt(25)=5,sqrt(26)也是5。打个比方,遍历的时候,面积是25,i=3,i^2=9,sqrt(25-9)=4。再反过来算3^2+4^2==25。假如i=2,2^2=4,,sqrt(25-4)=4。再返回来算2^2+4^2=20!=25。所以利用sqrt取整的特性算。
Accepted Code
#include<iostream>#include<algorithm>using namespace std;int main(){ int s; while(cin>>s){ int r=sqrt(s); int res=0; if(r*r==s){ res+=4; r--; } for(int i=1;i<=r;i++){ int j=sqrt(s-i*i); if((j*j+i*i) == s) res+=4; } cout<<res<<endl; } return 0;}
阅读全文
0 0
- 优雅的点
- 优雅的点
- Nodejs优雅的点
- 优雅的点
- 优雅的点
- 网易---优雅的点
- 【简单】优雅的点
- 优雅的点
- 优雅的点
- 优雅的点
- 优雅的点
- 优雅的锚点定位
- [编程题] 优雅的点
- [编程题] 优雅的点
- 【算法题】优雅的点
- [编程题] 优雅的点
- [编程题]优雅的点
- 使用优雅的锚点开发ios
- HDU 6203 ping ACM/ICPC 2017 Shenyang Online(LCA+贪心)
- [LeetCode] 128: Valid Sudoku
- [LeetCode] 129: Validate Binary Search Tree
- [LeetCode] 130: Candy
- [LeetCode] 131: Clone Graph
- 优雅的点
- [LeetCode] 132: Copy List with Random Pointer
- Linux的有线网络 无线网络连接配置与解决方案
- [LeetCode] 133: Gas Station
- 使用dos编译java文件出现编码出错
- [LeetCode] 134: Wildcard Matching
- 1002. A+B for Polynomials (25)
- [LeetCode] 135: Word Break
- [LeetCode] 136: Word Break II