孩子们!!!
现在大佬来拯救你们了!
相信大家都能想到这里:
//普通代码
cin >> a;
for(int i = 1; i <= a; i++){
for(int j = 1; j <= a; j++){
if(i * i + j * j == a) cout << "Yes\n";
}
}
可是TLE啊!超时!
你想想,如果a为10的6次方,你这循化就要要运行10的12次方次,不超时才怪!
那我们不妨想一下,i * i + j * j == n,那i * i和j * j都肯定不会超过n,那这时我们就可以简化这个代码了!
//改后代码
cin >> a;
for(int i = 1; i <= a; i++){
for(int j = 1; j <= a; j++){
if(i * i + j * j == a) cout << "Yes\n"
}
}
明白了吗?
下面是作者本人的代码,仅供参考
#include <bits/stdc++.h> //万能头
using namespace std;
int is(int x){ //热爱自定义函数
for(int i = 1; i * i <= x - 1; i++){ //循环
for(int j = 1; j * j <= x; j++){
if(i * i + j * j == x) return 1;
}
}
return 0;
}
int main(){//主程序
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int a;
cin >> a;
if(is(a)) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
制作不易,关注一下作者吧!
以后还会再帮助大家哦