Pythagorean Theorem II CodeForces - 304A 优化

题解:

首先,纯暴力三层循环写,不做任何优化剪枝,AC想都不要想;

然后我就试了下用map写,(因为这是STL集训的一道题)将a,b平方和用map存起来,然后枚举c的值,与其比较;然而,这样的代码连n=1000都要跑好久,n=10000就根本想都不要想。

代码差不多是这样:

#include <map>  
  
#include <string>  
  
#include <iostream>  
  
using namespace std;  
  
int main()  
  
{  
  
    map<int, string> mapStudent;  
  
    pair<map<int, string>::iterator, bool> Insert_Pair;  
  
    Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));  
  
    if(Insert_Pair.second == true)  
  
        cout<<"Insert Successfully"<<endl;  
  
    else  
  
        cout<<"Insert Failure"<<endl;  
  
    Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));  
  
    if(Insert_Pair.second == true)  
  
        cout<<"Insert Successfully"<<endl;  
  
    else  
  
        cout<<"Insert Failure"<<endl;  
  
    map<int, string>::iterator iter;  
  
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  
       cout<<iter->first<<' '<<iter->second<<endl;  
  
}  

 后来,仔细想想,WTF,水题。

用 sqrt(a*a+b*b)与c的值相比,如果相同,那么阔以,不然不行。

正解:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int ans=0;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<i;j++)
		{
			int t=i*i+j*j;
			int c=sqrt(t);
			if(c*c!=t) continue;
			if(c>n) continue;
			if(i+j<=c||i+c<=j||c+j<=i) continue;
			ans++;
		}
	}
	cout<<ans<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值