题目大意:给出一个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. MVC中页面传值方式总结

    MVC中的页面传值,通常指Controller和view之间的数据传递,经常用到的有几种方式,总结如下: 一.Controller----------->View(控制器传到视图) 1.View ...

  2. Spring-----AOP深度理解

    AOP定义了一些新的概念,要想深入的理解AOP的原理,就必须掌握这些概念的具体含义,本人菜鸡一枚,一下是自己对一些概念的理解,如果哪里不对,欢迎评论区指正 AOP核心概念AOP即Aspect-Orie ...

  3. sql中非存储过程定义参数并使用

    DECLARE @dt datetime SET @dt=GETDATE()--1.短日期格式:yyyy-m-d SELECT REPLACE(CONVERT(varchar(10),@dt,120) ...

  4. 点击一个元素,触发另一个元素的click事件

  5. ABP学习入门系列(四)(创建Service)

    一,一些相关解释 Service 在应用服务层也就是application层.应用服务用于将领域(业务)逻辑暴露给展现层.展现层通过传入DTO(数据传输对象)参数来调用应用服务,而应用服务通过领域对象 ...

  6. iOS交互h5——user-agent

    User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. 在公司产品中,在userAgent中增加了XX ...

  7. EF 多种查询方式

    比较常用的查询方式linq to entity,这里先看一种写法: var query = (from d in testContext.Set<DepartPerson>() //查询和 ...

  8. mysql 查询每秒写入数据库的记录数

    SELECT * from t_user ORDER BY create_time desc SELECT create_time,  COUNT(create_time) as num from t ...

  9. libevent2笔记(linux、windows、android的编译)

    0. 前言 我使用的版本是libevent-2.0.21-stable.高级的应用还是得看官网文档http://www.wangafu.net/~nickm/libevent-2.0/doxygen/ ...

  10. 递归方程T(n)=aT(n/b)+f(n)之通用解法

    ,b>1为常数,f(n)为函数,T(n)=aT(n/b)+f(n)为非负数,令x=logba: 1.       f(n)=o(nx-e),e>0,那么T(n)=O(nx). 2.     ...