http://acm.hdu.edu.cn/showproblem.php?pid=1407

计算方程x^2+y^2+z^2= num的一个正整数解。num为不大于10000的正整数

思路:

方法一、直接三重循环暴力 234MS

方法二、枚举x和y,对z进行二分查找。62MS

方法三、枚举x和y,对z用hash判断是否存在 15MS

方法二和方法三都是枚举x和y,但是二分查找为O(log(n))而hash为O(1)

可以看到,方法一和方法三差了十几倍!

还有就是用goto从内重循环直接跳出简洁而优雅。

方法一:直接三重循环暴力 234MS

  1. #include<cstdio>
  2. #include<cmath>
  3. int res[101];
  4. int main()
  5. {
  6. int n;
  7. for(int i=1;i<=100;i++)
  8. res[i]=i*i;
  9.  
  10. while(~scanf("%d",&n))
  11. {
  12. for(int x=1;x<=100;x++)
  13. for(int y=1;y<=100;y++)
  14. for(int z=1;z<=100;z++)
  15. if( res[x]+res[y]+res[z]==n)
  16. {
  17. printf("%d %d %d\n",x,y,z);
  18. goto end;
  19. }
  20. end:;
  21. }
  22. return 0;
  23. }

方法二:枚举x和y,对z进行二分查找。62MS

  1. #include<cstdio>
  2. #include<cmath>
  3. int res[101];
  4. int search(int n)
  5. {
  6. int L=1,R=101;
  7. while(L<R)
  8. {
  9. int m=(L+R)>>1;
  10. if(res[m]==n)
  11. return m;
  12. else if(res[m] < n)
  13. L=m+1;
  14. else
  15. R=m;
  16. }
  17. return -1;
  18. }
  19. int main()
  20. {
  21. int n;
  22. for(int i=1;i<=100;i++)
  23. res[i]=i*i;
  24.  
  25. while(~scanf("%d",&n))
  26. {
  27. for(int x=1;x<=100;x++)
  28. {
  29. for(int y=1;res[x]+res[y]<n;y++)
  30. {
  31. int z=search(n-res[x]-res[y]);
  32. if(z!=-1)
  33. {
  34. printf("%d %d %d\n",x,y,z);
  35. goto end;
  36.  
  37. }
  38. }
  39. }
  40.  
  41. end:;
  42. }
  43. return 0;
  44. }

方法三:枚举x和y,对z用hash判断是否存在  15MS

  1. #include<cstdio>
  2. #include<cmath>
  3. const int MAXN=10001;
  4. int res[101];
  5. struct HASH
  6. {
  7. bool exist;
  8. int index;
  9. }hash[MAXN];
  10. int main()
  11. {
  12. int n;
  13. for(int i=1;i<=100;i++)
  14. {
  15. res[i]=i*i;
  16. hash[ res[i] ].exist=true;
  17. hash[ res[i] ].index=i;
  18. }
  19.  
  20. while(~scanf("%d",&n))
  21. {
  22. for(int x=1;x<=100;x++)
  23. {
  24. for(int y=1;res[x]+res[y]<n;y++)
  25. {
  26. int id=n-res[x]-res[y];
  27. if(hash[id].exist)
  28. {
  29. printf("%d %d %d\n",x,y,hash[id].index);
  30. goto end;
  31.  
  32. }
  33. }
  34. }
  35.  
  36. end:;
  37. }
  38. return 0;
  39. }

HDU 1407 测试你是否和LTC水平一样高 枚举、二分、hash的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    题目大意:给出一个num,计算方程x^2+y^2+z^2 = num的第一个正整数解(字典序),0 < num <= 10000. 方法参考了网上的博客,自己打了一波,发现还有很多不懂的地 ...

  9. HDU - 1407 打表

    思路:预处理10000以内所有数的三平方和即可. AC代码 #include <cstdio> #include <cmath> #include <algorithm& ...

随机推荐

  1. js中的组合模式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 【Django】ContentType组件

    目录 理解 表结构 使用 @ 好,现在我们有这样一个需求,我们的商城里有很多的商品,然而节日要来了,我们要搞活动. 那么,我们就要设计优惠券,优惠券都有什么类型呢?满减的.折扣的.立减的.等等等... ...

  3. Install Docker Mac OS X

    检查 Mac OS version 要求必须是 OS X 10.6 Snow Leopard or newer to run Boot2Docker 安装 Boot2Docker 列表内容 下载地址: ...

  4. 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...

  5. PatentTips - Supporting address translation in a virtual machine environment

    BACKGROUND A conventional virtual-machine monitor (VMM) typically runs on a computer and presents to ...

  6. 洛谷 P2347 砝码称重

    P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...

  7. ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第四篇:传递表单数据

    摘要      本文将完成我们“MVC公告发布系统”的公告发布功能,以此展示在ASP.NET MVC中如何传递处理表单的数据. 前言      通过前几篇文章,我们已经能比较自如的使用ASP.NET ...

  8. 一个很详细的web.xml讲解(转)

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  9. 福昕pdf阅读器如何删除所有注释

    然后选中第一个 移动到最后按住shift,选择最后一个, 总之就是选中所有的 然后右键,点击删除即可. 不要忘记保存呦

  10. VUE笔记 - 品牌后台 - v-for Splice Some Filter findIndex indexOf 直接return函数结果

    <body> <div id="app"> <div class="panel panel-primary"> <di ...