POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数
POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 21708 | Accepted: 9708 |
Description
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
Input
Output
Sample Input
1
2
9
0
Sample Output
1
2
10
找质因子为2、3、5的数,实际这些数就是2、3、5这些数互相乘。从大到小排好序的序列。
发现这样的题也有一个固定的套路,也渐渐知道模板题是个什么概念了。
代码:
#include <iostream>
using namespace std; int main()
{
int a[1500]={1},i=1,j2=0,j3=0,j5=0,m,count;
while(i<1500)
{
m=999999999;
if(m>2*a[j2])m=2*a[j2];
if(m>3*a[j3])m=3*a[j3];
if(m>5*a[j5])m=5*a[j5];
if(m==2*a[j2])j2++;
if(m==3*a[j3])j3++;
if(m==5*a[j5])j5++;
a[i]=m;
i++;
}
while(cin>>count&&count)
{
cout<<a[count-1]<<endl;
}
return 0;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6560 | Accepted: 3010 |
Description
For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, ...
So H5(2, 3, 5)=6.
Input
Output
Sample Input
7 13 19 100
Sample Output
26590291
和前面的题目一个意思,一開始的问题在于输入不超过10^18,心想这不是搞笑吗,又要TLE了。求出来打表?后来发现输出也要不超过10^18。于是就试试,结果成了。
代码:
#include <iostream>
#pragma warning(disable:4996)
using namespace std;
#define MAXN 10006 long long a[MAXN]; int main()
{
a[0] = 1;
int i=1,i2,j1=0,j2=0,j3=0,p1,p2,p3;
long long m;
cin>>p1>>p2>>p3>>i2;
while(i<=10005)
{
m=9223372036854775807;
if(m>p1*a[j1]) m=p1*a[j1];
if(m>p2*a[j2]) m=p2*a[j2];
if(m>p3*a[j3]) m=p3*a[j3]; if(m==p1*a[j1])j1++;
if(m==p2*a[j2])j2++;
if(m==p3*a[j3])j3++;
a[i]=m;
i++;
} cout<<a[i2]<<endl;
return 0;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9509 | Accepted: 4465 |
Description
(1) 1 is in S;
(2) If x is in S, then 2x + 1 and 3x + 1 are also in S;
(3) No other element belongs to S.
Find the N-th element of set S, if we sort the elements in S by increasing order.
Input
Output
Sample Input
100
254
Sample Output
418
1461
代码:
#include <iostream>
#pragma warning(disable:4996)
using namespace std; int a[10000005]; int main()
{
a[0] = 1;
int i=1,j2=0,j3=0;
long long m;
while(i<=10000000)
{
m=9223372036854775807;
if(m>2*a[j2])m=2*a[j2]+1;
if(m>3*a[j3])m=3*a[j3]+1; if(m==2*a[j2]+1)j2++;
if(m==3*a[j3]+1)j3++;
a[i]=m;
i++;
}
while(scanf("%d",&i)==1)
{
cout<<a[--i]<<endl;
}
return 0;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9951 | Accepted: 4651 |
Description
Write a program to find and print the nth element in this sequence.
Input
Output
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
做到这里就对这样的题非常烦了。
。。
代码:
#include <iostream>
#pragma warning(disable:4996)
using namespace std;
#define MAXN 10006 long long a[MAXN]; int main()
{
a[1] = 1;
int i=1,i2,j1=1,j2=1,j3=1,j4=1;
long long m; while(i<=5842)
{
m=4000000000;
if(m>2*a[j1]) m=2*a[j1];
if(m>3*a[j2]) m=3*a[j2];
if(m>5*a[j3]) m=5*a[j3];
if(m>7*a[j4]) m=7*a[j4]; if(m==2*a[j1])j1++;
if(m==3*a[j2])j2++;
if(m==5*a[j3])j3++;
if(m==7*a[j4])j4++;
a[++i]=m;
}
while(cin>>i2)
{
if(!i2)
break;
if((i2%100)>=10&&(i2%100)<=20)
{
cout<<"The "<<i2<<"th humble number is "<<a[i2]<<"."<<endl;
}
else if(i2%10==1)
{
cout<<"The "<<i2<<"st humble number is "<<a[i2]<<"."<<endl;
}
else if(i2%10==2)
{
cout<<"The "<<i2<<"nd humble number is "<<a[i2]<<"."<<endl;
}
else if(i2%10==3)
{
cout<<"The "<<i2<<"rd humble number is "<<a[i2]<<"."<<endl;
}
else
{
cout<<"The "<<i2<<"th humble number is "<<a[i2]<<"."<<endl;
}
} return 0;
}
所以总结一下的话。由于按一条一条的要求逐渐去查找,前一个数又作为查找后一个数的基础,所以有多少条件就搞多少个j1,j2,j3。取最小的那个,之后选择了哪一个条件,就将相应条件的jn+1。让它到队列的下一个,接着推断,逐渐得到一整个数的序列。
POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数的更多相关文章
- ytu 1061: 从三个数中找出最大的数(水题,模板函数练习 + 宏定义练习)
1061: 从三个数中找出最大的数 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 124[Submit][Status][We ...
- c# 各种排序算法+找第二大的数+句子单词反转
冒泡排序 // 冒泡排序 bubble sort public static int[] BubbleSort(int []array) { bool isContinue = true; ; i & ...
- 与班尼特·胡迪一起找简单规律(HZOJ-2262)
与班尼特·胡迪一起找简单规律 Time Limit: 1 s Memory Limit: 256 MB Description 班尼特·胡迪发现了一个简单规律 给定一个数列,1 , 1 ...
- 如何在PHP页面中原样输出HTML代码(是该找本php的数来看了)
如何在PHP页面中原样输出HTML代码(是该找本php的数来看了) 一.总结 一句话总结:字符串与HTML之间的相互转换主要应用htmlentities()函数来完成. 1.php中的html标签如何 ...
- LeetCode Find the Duplicate Number 找重复出现的数(技巧)
题意: 有一个含有n+1个元素的数组,元素值是在1-n之间的整数,请找出其中出现超过1次的数.(保证仅有1个出现次数是超过1的数) 思路: 方法一:O(nlogn).根据鸽笼原理及题意,每次如果< ...
- hdu 4925 贪心 自己从小到大做数据找方法规律
http://acm.hdu.edu.cn/showproblem.php?pid=4925 自己逐个做数据找规律.提供下我的找的: 1 2 1 3 2 2 2 3 3 3 然后发现这种矩阵是最优的: ...
- 找第二大的数SQL-Second Highest Salary
1: 找小于最大的最大的 select max(Salary) from Employee where Salary<(select MAX(Salary) from Employee); 2. ...
- 【算法】—— 1到n中减少了一个数,顺序被打乱,找出缺失的数
问题 有0-n这n+1个数,但是其中丢了一个数,请问如何找出丢了哪个数? 五种方法 1)用1+2+...+n减去当前输入数据的总和.时间复杂度:O(n) 空间复杂度:O(1) [容易溢出] 2)用12 ...
- 【模板】BM算法(找线性规律万能模板)
(1) n是指要找该数列的第n项. (2) 往vec中放入该数列前几项的值,越多越精确. #include<set> #include<cmath> #include<v ...
随机推荐
- myeclipse导入工程 Some projects cannot be imported because they already exist in the workspace
问题描述: 1 第一次从外部导入工程或者新建工程,成功: 2 删除该工程,但是没有选择delete project contents on disk 3 再次需要该工程,导入该工程时出现警告:Some ...
- Android 混淆后的代码调试
ProGuard的输出文件及用处 混淆之后,会给我们输出一些文件,在gradle方式下是在<project_dir>/build/proguard/目录下,ant是在<project ...
- HTML---经常使用标签总结与实践
什么是HTML? 超文本标记语言,标准通用标记语言下的一个应用. "超文本"就是指页面内能够包括图片.链接,甚至音乐.程序等非文字元素. 超文本标记语言的结构包含" ...
- Android动态载入JAR包的实现方法
有时候我们须要使用动态更新技术,简单来说就是:当我们把开发jar包发送给用户后.假设后期升级了部分代码.这时让用户的jar包自己主动更新,而不是用户主动手动地去更新的技术就是动态更新.这个须要使用的技 ...
- [JZOJ 5906] [NOIP2018模拟10.15] 传送门 解题报告(树形DP)
题目链接: https://jzoj.net/senior/#contest/show/2528/2 题目: 8102年,Normalgod在GLaDOS的帮助下,研制出了传送枪.但GLaDOS想把传 ...
- 132.try throw catch介绍
#include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...
- P3369 【模板】普通平衡树(Treap/SBT)(pb_ds版)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(若有多个相同的数,因输出最小的排名) 查询 ...
- SVN Commit报错 svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted
svn commit 文件出错 svn: E155037: Commit failed (details follow): svn: E155037: Previous operation has n ...
- label标签的可访问性问题
label标签可以优雅地扩大表单控件元素的点击区域.例如,单纯的单选框点击区域就鼻屎那么大的地方,经常会点不到位置.因此,label标签的使用对于提高页面的可用性可访问性是很有帮助的. 其实,它的样子 ...
- SQL 中多个 and or 的组合运算
sql关系型运算符优先级高到低为:not >and> or AND.OR运算符的组合使用 在WHERE子句中,通过AND.OR运算符可以同时连接多个条件,当然AND.OR运算符也可以同时使 ...