POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结

POJ1338:Ugly Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21708   Accepted: 9708

Description

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 

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

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

Output

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

Sample Input

  1. 1
  2. 2
  3. 9
  4. 0

Sample Output

  1. 1
  2. 2
  3. 10

找质因子为2、3、5的数,实际这些数就是2、3、5这些数互相乘,从大到小排好序的序列。

发现这种题也有一个固定的套路,也渐渐知道模板题是个什么概念了。

代码:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int a[1500]={1},i=1,j2=0,j3=0,j5=0,m,count;
  7. while(i<1500)
  8. {
  9. m=999999999;
  10. if(m>2*a[j2])m=2*a[j2];
  11. if(m>3*a[j3])m=3*a[j3];
  12. if(m>5*a[j5])m=5*a[j5];
  13. if(m==2*a[j2])j2++;
  14. if(m==3*a[j3])j3++;
  15. if(m==5*a[j5])j5++;
  16. a[i]=m;
  17. i++;
  18. }
  19. while(cin>>count&&count)
  20. {
  21. cout<<a[count-1]<<endl;
  22. }
  23. return 0;
  24. }

POJ2545:Hamming Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6560   Accepted: 3010

Description

For each three prime numbers p1, p2 and p3, let's define Hamming sequence Hi(p1, p2, p3), i=1, ... as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3. 



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

In the single line of input file there are space-separated integers p1 p2 p3 i.

Output

The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.

Sample Input

  1. 7 13 19 100

Sample Output

  1. 26590291

和前面的题目一个意思,一开始的问题在于输入不超过10^18,心想这不是搞笑吗,又要TLE了。求出来打表?后来发现输出也要不超过10^18。于是就试试,结果成了。

代码:

  1. #include <iostream>
  2. #pragma warning(disable:4996)
  3. using namespace std;
  4. #define MAXN 10006
  5.  
  6. long long a[MAXN];
  7.  
  8. int main()
  9. {
  10. a[0] = 1;
  11. int i=1,i2,j1=0,j2=0,j3=0,p1,p2,p3;
  12. long long m;
  13. cin>>p1>>p2>>p3>>i2;
  14. while(i<=10005)
  15. {
  16. m=9223372036854775807;
  17. if(m>p1*a[j1]) m=p1*a[j1];
  18. if(m>p2*a[j2]) m=p2*a[j2];
  19. if(m>p3*a[j3]) m=p3*a[j3];
  20.  
  21. if(m==p1*a[j1])j1++;
  22. if(m==p2*a[j2])j2++;
  23. if(m==p3*a[j3])j3++;
  24. a[i]=m;
  25. i++;
  26. }
  27.  
  28. cout<<a[i2]<<endl;
  29. return 0;
  30. }

POJ2591:Set Definition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9509   Accepted: 4465

Description

Set S is defined as follows: 

(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

Input will contain several test cases; each contains a single positive integer N (1 <= N <= 10000000), which has been described above.

Output

For each test case, output the corresponding element in S.

Sample Input

  1. 100
  2. 254

Sample Output

  1. 418
  2. 1461

代码:

  1. #include <iostream>
  2. #pragma warning(disable:4996)
  3. using namespace std;
  4.  
  5. int a[10000005];
  6.  
  7. int main()
  8. {
  9. a[0] = 1;
  10. int i=1,j2=0,j3=0;
  11. long long m;
  12. while(i<=10000000)
  13. {
  14. m=9223372036854775807;
  15. if(m>2*a[j2])m=2*a[j2]+1;
  16. if(m>3*a[j3])m=3*a[j3]+1;
  17.  
  18. if(m==2*a[j2]+1)j2++;
  19. if(m==3*a[j3]+1)j3++;
  20. a[i]=m;
  21. i++;
  22. }
  23. while(scanf("%d",&i)==1)
  24. {
  25. cout<<a[--i]<<endl;
  26. }
  27. return 0;
  28. }

POJ2247:Humble Numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9951   Accepted: 4651

Description

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 



Write a program to find and print the nth element in this sequence. 

Input

The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.

Sample Input

  1. 1
  2. 2
  3. 3
  4. 4
  5. 11
  6. 12
  7. 13
  8. 21
  9. 22
  10. 23
  11. 100
  12. 1000
  13. 5842
  14. 0

Sample Output

  1. The 1st humble number is 1.
  2. The 2nd humble number is 2.
  3. The 3rd humble number is 3.
  4. The 4th humble number is 4.
  5. The 11th humble number is 12.
  6. The 12th humble number is 14.
  7. The 13th humble number is 15.
  8. The 21st humble number is 28.
  9. The 22nd humble number is 30.
  10. The 23rd humble number is 32.
  11. The 100th humble number is 450.
  12. The 1000th humble number is 385875.
  13. The 5842nd humble number is 2000000000.

做到这里就对这种题很烦了。。。

代码:

  1. #include <iostream>
  2. #pragma warning(disable:4996)
  3. using namespace std;
  4. #define MAXN 10006
  5.  
  6. long long a[MAXN];
  7.  
  8. int main()
  9. {
  10. a[1] = 1;
  11. int i=1,i2,j1=1,j2=1,j3=1,j4=1;
  12. long long m;
  13.  
  14. while(i<=5842)
  15. {
  16. m=4000000000;
  17. if(m>2*a[j1]) m=2*a[j1];
  18. if(m>3*a[j2]) m=3*a[j2];
  19. if(m>5*a[j3]) m=5*a[j3];
  20. if(m>7*a[j4]) m=7*a[j4];
  21.  
  22. if(m==2*a[j1])j1++;
  23. if(m==3*a[j2])j2++;
  24. if(m==5*a[j3])j3++;
  25. if(m==7*a[j4])j4++;
  26. a[++i]=m;
  27. }
  28. while(cin>>i2)
  29. {
  30. if(!i2)
  31. break;
  32. if((i2%100)>=10&&(i2%100)<=20)
  33. {
  34. cout<<"The "<<i2<<"th humble number is "<<a[i2]<<"."<<endl;
  35. }
  36. else if(i2%10==1)
  37. {
  38. cout<<"The "<<i2<<"st humble number is "<<a[i2]<<"."<<endl;
  39. }
  40. else if(i2%10==2)
  41. {
  42. cout<<"The "<<i2<<"nd humble number is "<<a[i2]<<"."<<endl;
  43. }
  44. else if(i2%10==3)
  45. {
  46. cout<<"The "<<i2<<"rd humble number is "<<a[i2]<<"."<<endl;
  47. }
  48. else
  49. {
  50. cout<<"The "<<i2<<"th humble number is "<<a[i2]<<"."<<endl;
  51. }
  52. }
  53.  
  54. return 0;
  55. }

所以总结一下的话,因为按一条一条的要求逐渐去查找,前一个数又作为查找后一个数的基础,所以有多少条件就搞多少个j1,j2,j3。取最小的那个,之后选择了哪一个条件,就将对应条件的jn+1,让它到队列的下一个,接着判断,逐渐得到一整个数的序列。

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ1338 & POJ2545 & POJ2591 & POJ2247的更多相关文章

  1. POJ1338 &amp; POJ2545 &amp; POJ2591 &amp; POJ2247 找给定规律的数

    POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结 POJ1338:Ugly Numbers Time Limit: 1000MS   Memory Limit: 10 ...

  2. [POJ1338]Ugly Numbers

    [POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

  3. [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 ...

  4. 【POJ1338】Ugly Numbers(暴力打表)

    打表大军是一股邪恶势力→_→ #include <iostream> #include <cstring> #include <cstdlib> #include ...

  5. poj1338

                                                                                 Ugly Numbers Time Limit ...

  6. 4.2 例题: 统计字符数 poj2247

    问题描述 判断一个由 a-z 这 26 个字符组成的字符串中哪个字符出现的次数最多 输入:第 1 行是测试数据的组数 n,每组测试数据占 1 行,是一个由 a-z 这 26 个字符组 成的字符串,每组 ...

  7. poj1338 Ugly Numbers 打表, 递推

    题意:一个数的质因子能是2, 3, 5, 那么这个数是丑数. 思路: 打表或者递推. 打表: 若该数为丑数,那么一定能被2 或者3, 或者5 整除, 除完之后则为1. #include <ios ...

  8. poj1338【丑数·DP】

    我记得这道题以前写过,而且是写出来了.DP吧. 然后现在想了好久...没想出来.... 然后考虑一下递推..mdzz-直接就是让之前的这个每次乘以2,3,5就好了嘛,然后每轮取最小. //#inclu ...

  9. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

随机推荐

  1. PyCharm 2018.1破解激活步骤

    1.下载破解补丁 下载地址:https://pan.baidu.com/s/1qjI9uHaw0x374rwu6H8djA 将下载下来的破解补丁放到C:\software\JetBrains\PyCh ...

  2. php 投票系统

    1.投票主界面(问题界面) <?php$db = new Mysqli("localhost","root","root"," ...

  3. 本地VMware虚拟机环境CentOS7.6 安装宝塔Linux面板

    之前测试一直都在使用phpenv和phpstudy ,不过这两个集成环境时长容易出bug,各种问题劝退.之前Windows开始Linux的体验又不尽人意,今天介绍一个别的方法.VMware安装Linu ...

  4. Censoring「USACO 2015 Feb」

    题目描述 有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程. 输入格式 包含两行,第一行为S ...

  5. 免费的 Linux 分区管理器使用介绍

    下面的列表没有特定的排名顺序.大多数分区工具应该存在于 Linux 发行版的仓库中. GParted 这可能是 Linux 发行版中最流行的基于 GUI 的分区管理器.你可能已在某些发行版中预装它.如 ...

  6. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  7. Phoenix与HBase集成进行数据分析

    安装好Phoenix后配置环境变量 export PHOENIX_PATH=/opt/cloudera/parcels/APACHE_PHOENIX-4.14.0-cdh5.14.2.p0.3expo ...

  8. 07.swoole学习笔记--tcp客户端

    <?php //创建tcp客户端 $client=new swoole_client(SWOOLE_SOCK_TCP); //连接服务器 $client->connect(,) or di ...

  9. 005.CI4框架CodeIgniter, 通过带路径的view视图访问

    01. 我们在app目录的Views文件夹中新建一个Login文件,再创建一个login.php文件,在里面我们写上如下代码: <!doctype html> <html> & ...

  10. Oracle自动备份bat

    很多时候我们需要自动备份数据库这边推荐bat+Windows计划任务实现 方案1 创建以下bat 然后添加到TaskSchedule(路径最好不要包含中文) @echo off @echo ===== ...