HDU-计算机学院大学生程序设计竞赛(2015’11)1004 质方数(打表)
来源:互联网 发布:第七感双色球软件 编辑:程序博客网 时间:2024/05/22 06:24
1001-搬砖(贪心):http://blog.csdn.net/idealism_xxm/article/details/50097051
1002-投币洗衣机(模拟):http://blog.csdn.net/idealism_xxm/article/details/50097231
1003-玩骰子(枚举) :http://blog.csdn.net/idealism_xxm/article/details/50097691
1005-ACM组队安排(排列组合):http://blog.csdn.net/idealism_xxm/article/details/50097801
1006-逆袭指数(枚举):http://blog.csdn.net/idealism_xxm/article/details/50098201
1007-油菜花王国(并查集):http://blog.csdn.net/idealism_xxm/article/details/50097473
1008-游乐场(贪心):http://blog.csdn.net/idealism_xxm/article/details/50097577
质方数
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description
小明天生对数字比较敏感,3岁的时候就能背诵圆周率一百位。
现在,小明慢慢长大了,但依然很喜欢数字,最近,他迷上了质数和平方数,并且自己把质数的平方命名为“质方数”。
现在,他在研究这样一个问题:距离一个正整数N最接近的质方数是多少?
现在,小明慢慢长大了,但依然很喜欢数字,最近,他迷上了质数和平方数,并且自己把质数的平方命名为“质方数”。
现在,他在研究这样一个问题:距离一个正整数N最接近的质方数是多少?
Input
输入数据第一行是一个正整数T(T<=20),表示有T组输入数据。
接下来T行,每行输入一个正整数N(1<=N<=10^8)。
接下来T行,每行输入一个正整数N(1<=N<=10^8)。
Output
对于每组数据,请输出距离N最接近的质方数,每组输出占一行。
Sample Input
2110
Sample Output
49
在水题中,也总想着优化时间,明明枚举更简单。
由于1<=n<=10^8,所以只需用找到10^4以内的质数(共1229个),防止极限数据出错,需要找到大于10^4的第一个质数10007
将所有的质数的平方按升序存入质方数表
每次读入数n后,找到大于等于n的第一个质方数lst[index],然后在lst[index]与lst[index-1]中取离n更近的数
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int T,n,ans;bool notPrime[10009];int lst[1235],num,index;void init() {//初始化质方数表 memset(notPrime,false,sizeof(notPrime)); notPrime[1]=notPrime[0]=true; for(int i=2;i<=5005;++i) for(int j=2;i*j<10009;++j) notPrime[i*j]=true; num=0; for(int i=0;i<10009;++i) if(!notPrime[i]) lst[num++]=i*i;}int main() { init(); scanf("%d",&T); while(T--) { scanf("%d",&n); index=lower_bound(lst,lst+num,n)-lst;//找到大于等于n的第一个数的下表 if(index==0||n-lst[index-1]>lst[index]-n)//在lst[index]和lst[index-1]中找离n更近的质方数 printf("%d\n",lst[index]); else printf("%d\n",lst[index-1]); } return 0;}
1001-搬砖(贪心):http://blog.csdn.net/idealism_xxm/article/details/50097051
1002-投币洗衣机(模拟):http://blog.csdn.net/idealism_xxm/article/details/50097231
1003-玩骰子(枚举) :http://blog.csdn.net/idealism_xxm/article/details/50097691
1004-质方数(打表):http://blog.csdn.net/idealism_xxm/article/details/50097255
1005-ACM组队安排(排列组合):http://blog.csdn.net/idealism_xxm/article/details/50097801
1006-逆袭指数(枚举):http://blog.csdn.net/idealism_xxm/article/details/50098201
1007-油菜花王国(并查集):http://blog.csdn.net/idealism_xxm/article/details/50097473
1008-游乐场(贪心):http://blog.csdn.net/idealism_xxm/article/details/50097577
1002-投币洗衣机(模拟):http://blog.csdn.net/idealism_xxm/article/details/50097231
1003-玩骰子(枚举) :http://blog.csdn.net/idealism_xxm/article/details/50097691
1004-质方数(打表):http://blog.csdn.net/idealism_xxm/article/details/50097255
1005-ACM组队安排(排列组合):http://blog.csdn.net/idealism_xxm/article/details/50097801
1006-逆袭指数(枚举):http://blog.csdn.net/idealism_xxm/article/details/50098201
1007-油菜花王国(并查集):http://blog.csdn.net/idealism_xxm/article/details/50097473
1008-游乐场(贪心):http://blog.csdn.net/idealism_xxm/article/details/50097577
0 0
- HDU-计算机学院大学生程序设计竞赛(2015’11)1004 质方数(打表)
- hdu 计算机学院大学生程序设计竞赛(2015’11)质方数
- 计算机学院大学生程序设计竞赛(2015’11)1004 质方数
- hdu 计算机学院大学生程序设计竞赛(2015’11)游乐场
- hdu--计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu计算机学院大学生程序设计竞赛(2015’11)1004 质方数
- 计算机学院大学生程序设计竞赛(2015’11)
- 计算机学院大学生程序设计竞赛(2015’11)
- 计算机学院大学生程序设计竞赛(2015’11)1008 游乐场
- 计算机学院大学生程序设计竞赛(2015’11)
- HDU-计算机学院大学生程序设计竞赛(2015’11)1001 搬砖(贪心)
- HDU-计算机学院大学生程序设计竞赛(2015’11)1002 投币洗衣机(模拟)
- HDU-计算机学院大学生程序设计竞赛(2015’11)1007 油菜花王国(并查集)
- HDU-计算机学院大学生程序设计竞赛(2015’11)1008 游乐场(贪心)
- 从零开始写javaweb框架笔记3-编写一个简单的web应用
- Content Provider之MIME类型
- Java使用局部变量的一些要点
- c++ to_String()
- codeforces Educational Codeforces Round 2 A. Extract Numbers
- HDU-计算机学院大学生程序设计竞赛(2015’11)1004 质方数(打表)
- Avro技术应用_1. Schema and Code Generation
- java/HashSet
- Ubuntu vim配置文件及系统安装
- 深入理解JavaBean内省
- 在Spring MVC中使用Velocity
- Notification方法更新
- Debian 7/Ubuntu14.04 LNMP环境搭建
- 矩阵乘法