A. Pythagorean Theorem II

A. Pythagorean Theorem II
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states:

In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle).

The theorem can be written as an equation relating the lengths of the sides ab and c, often called the Pythagorean equation:

a2 + b2 = c2

where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.

Given n, your task is to count how many right-angled triangles with side-lengths ab and c that satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n.

Input

The only line contains one integer n (1 ≤ n ≤ 104) as we mentioned above.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
5
output
1
input
74
output
35
题目很简单,只要一点技巧防止超时就能AC.下面说下题意:给你一个n,让你求出在 1 ≤ a ≤ b ≤ c ≤ n.即有多少个a2 + b2 = c2题意大概就是这样
我就说下注意点,就是不超时的技巧,可能你们刚拿到这道题,就会想到用三个for循环,那样是绝对超时的,因为三个for循环的时间复杂度是(On)^3,所以我们要把伞个for循环变一下,改成两个for循环,下面看代码就知道怎样使三个for循环变成两个了.
代码:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	int n,m,i,j,sum,b,c,p;
	double a;//为什么要单独让a是浮点型,因为这样便于判断是否满足a*a+b*b=c*c,即是开平方得到的a是否是一个整数.
	while(cin>>n)
	{
			sum=0;
			for(c=1;c<=n;c++)
			{
				for(b=1;b<c;b++)//这里的b不能等于c因为这样c每次循环下都会等到一个a等于0,a等于0不满足题意
				{
					p=c*c-b*b;
					a=sqrt(p);//这不是关键,因为如果不满足a*a+b*b=c*c,a一定是带小数点的,这样下面就好判断

					if(a==int(a)&&(a<=b))//还有注意题目a是小于或等于b的
					sum+=1;
				}
			}
		cout<<sum<<endl;
	}
	return 0;
}//如果有问题,或有什么疑惑,可以在评论中提出,小子我看到一定尽力解答

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值