1 Star 2 Fork 0

YangZhi1605/CSDNCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
四的平方和——数组模拟哈希法.cpp 857 Bytes
一键复制 编辑 原始数据 按行查看 历史
YangZhi1605 提交于 2022-02-21 21:51 +08:00 . csdn二分
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 5000010;
int h[N][3];
int n;
int main(int argc, char** argv) {
cin >> n;
for(int c = 0; c*c <= n; c++)
for(int d = c; d*d + c*c <= n; d++){
int tmp = d*d + c*c;
//哈希表中,当前位置没有数据才记录,倘若冲突了,也可以保证优先记录字典序较小的
if(h[tmp][0] == 0) {
h[tmp][0] = tmp;
h[tmp][1] = c;
h[tmp][2] = d;
}
}
for(int a = 0; a*a <= n; a++)
for(int b = a; b*b + a*a <= n; b++) {
int ans = n - a*a-b*b;
if(h[ans][0] != 0){
cout << a << " " << b << " " << h[ans][1] << " " << h[ans][2];
return 0;
}
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/twopointers/csdncode.git
git@gitee.com:twopointers/csdncode.git
twopointers
csdncode
CSDNCode
master

搜索帮助