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的更多相关文章
- POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数
POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结 POJ1338:Ugly Numbers Time Limit: 1000MS Memory Limit: 10 ...
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- 【POJ1338】Ugly Numbers(暴力打表)
打表大军是一股邪恶势力→_→ #include <iostream> #include <cstring> #include <cstdlib> #include ...
- poj1338
Ugly Numbers Time Limit ...
- 4.2 例题: 统计字符数 poj2247
问题描述 判断一个由 a-z 这 26 个字符组成的字符串中哪个字符出现的次数最多 输入:第 1 行是测试数据的组数 n,每组测试数据占 1 行,是一个由 a-z 这 26 个字符组 成的字符串,每组 ...
- poj1338 Ugly Numbers 打表, 递推
题意:一个数的质因子能是2, 3, 5, 那么这个数是丑数. 思路: 打表或者递推. 打表: 若该数为丑数,那么一定能被2 或者3, 或者5 整除, 除完之后则为1. #include <ios ...
- poj1338【丑数·DP】
我记得这道题以前写过,而且是写出来了.DP吧. 然后现在想了好久...没想出来.... 然后考虑一下递推..mdzz-直接就是让之前的这个每次乘以2,3,5就好了嘛,然后每轮取最小. //#inclu ...
- OJ题目分类
POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...
随机推荐
- Subtitles
1. 字幕Subtitles 2. 字幕类型 3. 字幕格式 4. 常用文本字幕 5. 字幕编辑器 6. 字幕编辑器比较 1. 字幕Subtitles https://en.wikipedia.org ...
- Day3-P - Matrix POJ3685
Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i ...
- Python递归函数如何写?正确的Python递归函数用法!
在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.一.举个例子,我们来计算阶乘n! = 1 x 2 x 3 x … x n,用函数fact(n)表示,可以看出:fac ...
- 「SCOI2010」幸运数字
传送门 Luogu 解题思路 首先构造出所有的幸运数字. 然后考虑一个幸运数字会产生多少贡献. 对于一个数 \(x\),它在区间 \([l,r]\) 内的倍数的个数为 \(\lfloor \frac{ ...
- jsp中获取attribute
value="<%=request.getSession().getAttribute("username")%>"
- 弱点扫描-openvas初始化
OPENVAS: NESSUS项目分支:商业版的扫描器 管理目标系统的漏洞 免费开源 Kali 默认安装但是未配置个启动 安装 创建证书 同步弱点数据库 创建客户端证书 重建数据库 备份数据库 启动服 ...
- Unbutu下装oracle
Ubuntu 16.04安装Oracle 11gR2入门教程图文详解 转自 https://www.linuxidc.com/Linux/2017-12/149797.htm 原文作 ...
- Arch-base-vs-iso
Arch-base-vs-iso 通常绝大多数的Linux分发版的iso镜像本身(*.iso文件都有约2Gb上下)都可以直接启动电脑并运行完整的Linux桌面系统. 极少数的Linux发行版仅提供命令 ...
- Ruoyi的确不错,不知后续能否坚持 允许商用
对于一个开源项目,作者的确很优秀: 在我们现在这个环境,能把一个开源项目做到这个规模,相当不容易:给作者点赞: 不过我也心里嘀咕,不知道后面哪天这个哥们突然发声明,不允许商用呢? 先偷偷留个证据,省的 ...
- 简述DDD,战略设计
从What.How.Why三个层面进行了梳理. What:DDD是什么?DDD是用来解决软件复杂度的问题,是一种软件思想. Why:为什么DDD可以解决软件复杂度?对于规模造成的复杂度,可以借助限界上 ...