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 ...
随机推荐
- [转]一步步学习EF Core(2.事务与日志)
本文转自:http://www.cnblogs.com/GuZhenYin/p/6862505.html 上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来 其实答案很简单,~ ...
- WPF Trigger for IsSelected in a DataTemplate for ListBox items
<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlugView /> ...
- elasticsearch 分布式集群搭建
elasticsearch环境搭建及单节点搭建可参考我的上一篇:http://www.cnblogs.com/xuwenjin/p/8745624.html 本文以Elaticsearch 6.2.2 ...
- Storm框架:如何实现crontab定时任务
Storm除了能对消息流进行处理,还能实现crontab定时任务. 只要在bolt中配置TOPOLOGY_TICK_TUPLE_FREQ_SECS项即可实现. @Override public Map ...
- Linux 添加定时任务,crontab -e 命令与直接编辑 /etc/crontab 文件
1. 使用 crontab -e 命令编辑定时任务列表 使用这个命令编辑的定时任务列表是属于用户级别的,初次编辑后在 /var/spool/cron 目录下生成一个与用户名相同的文件,文件内容就是我们 ...
- C#设计模式--代理模式(学习Learning hard 设计模式笔记)
class Program { static void Main(string[] args) { //创建一个代理对象 并发出请求 Person proxy = new Friend(); prox ...
- cf623A. Graph and String(二分图 构造)
题意 题目链接 Sol 可以这样考虑,在原图中没有边相连的点的值肯定是a / c 那么直接二分图染色即可 #include<bits/stdc++.h> #define LL long l ...
- js面向对象设计之function类
本文仅探讨如何合理的使用 function 在 javascript中实现一个面向对象设计的类.总所周知,javascript 并不能实现一个真正意义上的类,比如 protect 比如 函数重载.下面 ...
- Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug
这几天eclipse 项目迁移 android studio Error:Execution failed for task ':app:transformClassesWithJarMergingF ...
- OpenGL学习--06--键盘与鼠标交互
1.tutorial06.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> // I ...