题目大意:给出一个num,计算方程x2+y2+z^2 = num的第一个正整数解(字典序),0 < num <= 10000。

方法参考了网上的博客,自己打了一波,发现还有很多不懂的地方。

方法1:直接对x、y、z枚举。

用goto跳出多重循环

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <malloc.h>
using namespace std; int main() {
int n;
int pow2[105];
for (int i = 1; i <= 100; i++) {
pow2[i] = i*i;
}
while(!cin.eof() && cin >> n) {
for (int a = 1; a <= 100; a++) {
for (int b = 1; b <= 100; b++) {
for (int c = 1; c <= 100; c++) {
if (pow2[a] + pow2[b] + pow2[c] == n) {
cout << a << " " << b << " " << c << endl;
goto end;
}
}
}
}
end:;
} return 0;
}

方法2:对x、y枚举,对z使用二分查找。

分清楚m和a[m],勿乱。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <malloc.h>
using namespace std; int binSearch(int* a, int tar) {
int l = 1;
int r = 101;
while(l < r) {
int m = l + (r - l) / 2; //写成int m = (l + r) / 2可能会爆
if (a[m] == tar) return m;
else if (a[m] < tar) l = m + 1;
else r = m;
}
return -1;
} int main() {
int n;
int pow2[105];
for (int i = 1; i <= 100; i++) {
pow2[i] = i*i;
}
while(!cin.eof() && cin >> n) {
for (int a = 1; a <= 100; a++) {
for (int b = 1; b <= 100; b++) {
int c = binSearch(pow2, n - pow2[a] - pow2[b]);
if (c != -1) {
cout << a << " " << b << " " << c << endl;
goto end;
}
}
}
end:;
} return 0;
}

方法3:对x、y枚举,对z用hash查找。

普通数组:用key找value。hash:用value找key。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <malloc.h>
using namespace std; struct Hash {
bool exist;
int key;
};
Hash h[10001]; //很多头文件都有hash,所以改成了h int main() {
int pow2[105];
for (int i = 1; i <= 100; i++) {
pow2[i] = i*i;
h[pow2[i]].exist = true;
h[pow2[i]].key = i;
}
int n;
while(~scanf("%d", &n)) {
for (int a = 1; a <= 100; a++) {
for (int b = 1; pow2[a] + pow2[b] <= n; b++) { //b <= 100写法错误,id会小于0
int id = n - pow2[a] - pow2[b];
if(h[id].exist) {
printf("%d %d %d\n", a, b, h[id].key);
goto end;
}
}
}
end:;
} return 0;
}

HDU1407 测试你是否和LTC水平一样高的更多相关文章

  1. 测试你是否和LTC水平一样高[HDU1407]

    测试你是否和LTC水平一样高Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. HDU 1407 测试你是否和LTC水平一样高(枚举)

    测试你是否和LTC水平一样高 Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z ...

  3. B题 hdu 1407 测试你是否和LTC水平一样高

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1407 测试你是否和LTC水平一样高 Time Limit: 2000/1000 MS (Java/Ot ...

  4. hdu 1407 测试你是否和LTC水平一样高

    Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解.  Inpu ...

  5. 测试你是否和LTC水平一样高

    Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z^2= num的一个正整数解. ...

  6. HDOJ(HDU) 1407 测试你是否和LTC水平一样高(暴力)

    Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数 ...

  7. 解题报告:hdu 1407 测试你是否和LTC水平一样高

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1407 Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目 ...

  8. HDU 1407 测试你是否和LTC水平一样高 枚举、二分、hash

    http://acm.hdu.edu.cn/showproblem.php?pid=1407 计算方程x^2+y^2+z^2= num的一个正整数解.num为不大于10000的正整数 思路: 方法一. ...

  9. hibench 对CDH5.13.1进行基准测试(测试项目hadoop\spark\)HDFS作HA高可靠性

    使用CDH 5.13.1部署了HADOOP集群之后,需要进行基准性能测试. 一.hibench 安装 1.安装位置要求. 因为是全量安装,其中有SPARK的测试(SPARK2.0). 安装位置在SPA ...

随机推荐

  1. sqlserver把任意一列放到第一列并顺序排列

    请用一句sql写出将id为1234放到表的第一列,其他紧随其后并以正序排列的查询语句. 答案: select * from table where ID=2union all select * fro ...

  2. input 控件监听回车确认按钮。

    前端开发的同学捕捉回车按键经常会用到 if(event.keyCode == 13){ console.log("点击了回车按键");} 但是在微信上面,我们一般会用到指令 bin ...

  3. 装饰器模式(Decorator Pattern)

    装饰器模式 一.什么是装饰器模式   装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装 ...

  4. 如果你的shiro没学明白,那么应该看看这篇文章,将shiro整合进springboot

    最近在做项目的时候需要用到shiro做认证和授权来管理资源 在网上看了很多文章,发现大多数都是把官方文档的简介摘抄一段,然后就开始贴代码,告诉你怎么怎么做,怎么怎么做 相信很多小伙伴即使是跟着那些示例 ...

  5. GC详解及Minor GC和Full GC触发条件总结

    GC,即就是Java垃圾回收机制.目前主流的JVM(HotSpot)采用的是分代收集算法.与C++不同的是,Java采用的是类似于树形结构的可达性分析法来判断对象是否还存在引用.即:从gcroot开始 ...

  6. C#观察者模式的应用与思考

    1:项目场景 在设计数据表的时候有时候为了将来统计或查询的方便,我们会冗余一些字段.如有三张数据表,学校信息表.班级动态表.班级信息表. 班级动态由学校老师所发,可以进行评论点赞等操作,为了提升这种非 ...

  7. BZOJ5317:[JSOI2018]战争(闵可夫斯基和)

    令 \(a\in A,b\in B\) 则移动向量 \(\omega\) 使得存在 \(b+\omega=a\) 那么 \(\omega\) 需要满足 \(\omega=a−b\) 黑科技:闵可夫斯基 ...

  8. Django基础四之模板系统

    一 语法   模板渲染的官方文档 关于模板渲染你只需要记两种特殊符号(语法): {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二 变量 在Django的模板语言中按此语法使 ...

  9. vscode 代码跳转之PHP篇

    1.安装插件:PHP IntelliSense 2.配置:"php.executablePath": "C:\\php\\php.exe", 但是目前有问题,跨 ...

  10. PHP的new self() 与new static()

    参考链接:[PHP中new static()与new self()的区别异同分析],[PHP中new self()和new static()的区别探究],[PHP中static和self的区别] 要点 ...