HDU1407 测试你是否和LTC水平一样高
题目大意:给出一个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水平一样高的更多相关文章
- 测试你是否和LTC水平一样高[HDU1407]
测试你是否和LTC水平一样高Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1407 测试你是否和LTC水平一样高(枚举)
测试你是否和LTC水平一样高 Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z ...
- B题 hdu 1407 测试你是否和LTC水平一样高
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1407 测试你是否和LTC水平一样高 Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1407 测试你是否和LTC水平一样高
Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解. Inpu ...
- 测试你是否和LTC水平一样高
Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z^2= num的一个正整数解. ...
- HDOJ(HDU) 1407 测试你是否和LTC水平一样高(暴力)
Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数 ...
- 解题报告:hdu 1407 测试你是否和LTC水平一样高
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1407 Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目 ...
- HDU 1407 测试你是否和LTC水平一样高 枚举、二分、hash
http://acm.hdu.edu.cn/showproblem.php?pid=1407 计算方程x^2+y^2+z^2= num的一个正整数解.num为不大于10000的正整数 思路: 方法一. ...
- hibench 对CDH5.13.1进行基准测试(测试项目hadoop\spark\)HDFS作HA高可靠性
使用CDH 5.13.1部署了HADOOP集群之后,需要进行基准性能测试. 一.hibench 安装 1.安装位置要求. 因为是全量安装,其中有SPARK的测试(SPARK2.0). 安装位置在SPA ...
随机推荐
- 任务四十一:UI组件之日历组件(二)
任务四十一:UI组件之日历组件(二) 面向人群: 有一定基础的同学 难度: 中 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学 ...
- 主打安全 阿里巴巴联合公安部打造PMOS
4月9日,CITE2015(第三届中国电子信息博览会)在深圳会展中心举行,大会主要以“智能新时代.数字新生活”为主题.说道这,就不得不提国内互联网巨头阿里巴巴.大会中阿里巴巴不仅带来了早前马云曾经在德 ...
- Unity 发布到ios平台笔记
[ProjectName] was compiled with optimization - stepping may behave oddly; variables may not be avail ...
- vue setTimeout--延迟操作
有时候我们在查询后要做某些事情,例如我查询的时候要根据某个值再去查询某些东西并和这些值一起显示的时候,我们可以对渲染数据的操作进行延迟,因为代码执行的速度是很快的而访问数据的操作相对于渲染的速度慢得多 ...
- 湘潭校赛 Hard Wuxing
Hard Wuxing Accepted : 13 Submit : 166 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描述 “五行”是中国 ...
- php-fpm.conf 重要参数 max_children 和 request_terminate_timeout
php-fpm.conf 重要参数 max_children 和 request_terminate_timeout php-fpm.conf有两个至关重要的参数:一个是”max_children”, ...
- ASP.NET Core 2 学习笔记(十一)Cookies & Session
基本上HTTP是没有记录状态的协定,但可以通过Cookies将Request来源区分出来,并将部分数据暂存于Cookies及Session,是写网站常用的用户数据暂存方式.本篇将介绍如何在ASP.NE ...
- Review——JS的异步与同步
一.概念 同步(synchronous):指在js的主线程上,所有任务被依次执行: 异步(asynchronous):指任务不进入主线程,进入任务队列(task):当“任务队列”通知主线程,异步任务才 ...
- IOS微信后台运行时候倒计时暂停问题
链接:https://pan.baidu.com/s/1i7cSkqL 密码:g80i 最近给央视做了个H5答题游戏,但在倒计时上遇到一个终端问题:手机端按Home键将微信收入后台之后,IOS11 会 ...
- 使用JavaScript动态更改CSS样式
在很多情况下,都需要对网页上元素的样式进行动态的修改.在JavaScript中提供几种方式动态的修改样式,下面将介绍方法的使用.效果.以及缺陷. 1.使用obj.className来修改样式表的类名. ...