Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30212    Accepted Submission(s): 13229

Problem 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
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.
 

题意:输入n,输出第n个丑数

题解:丑数是可以由2,3,5,7,因子组成的,应用丑数是2,3,5,7的倍数这一性质来做。用动态规划,从第一个丑数1开始,第二个丑数就是前面的丑数乘以2,3,5,7后最小的那个值,就是2。

 #include<bits/stdc++.h>
using namespace std;
long long a[];
int main() {
int num=;
int p2,p3,p5,p7;
p2=p3=p5=p7=;
a[]=;
while(num<) {
a[++num]=min(min(a[p2]*,a[p3]*),min(a[p5]*,a[p7]*));
if(a[num]==*a[p2])//找到之后就在下面用if语句把能得到当前
p2++; // 丑数的情况去掉 继续找下一个
if(a[num]==*a[p3])
p3++;
if(a[num]==*a[p5])
p5++;
if(a[num]==*a[p7])
p7++;
}
int n;
while(~scanf("%d",&n),n)
{
if(n%==&&n%!=)
printf("The %dst humble number is %lld.\n",n,a[n]);
else if(n%==&&n%!=)
printf("The %dnd humble number is %lld.\n",n,a[n]);
else if(n%==&&n%!=)
printf("The %drd humble number is %lld.\n",n,a[n]);
else
printf("The %dth humble number is %lld.\n",n,a[n]);
} return ;
}

最新文章

  1. [转]Tesseract 3.02中文字库训练
  2. PHP数组合并+与array_merge的区别分析 &amp; 对多个数组合并去重技巧
  3. ActiveMQ初体验(转)
  4. BlockingQueue深入分析(转)
  5. 酷友观点/经验:支付接口返回数据接收地址,session数据丢失(或者说失效)的问题浅析(原创文章)
  6. 树形菜单的json字符串的拼接
  7. NavigationController的使用整理
  8. 一步步搭建自己的轻量级MVCphp框架-(一)什么是PHP框架以及MVC设计模式
  9. 微软提供的API的各个版本之间的区别
  10. 【POJ2155】【二维树状数组】Matrix
  11. 使用 GitHub+Hexo 搭建个人博客
  12. C语言编译过程及数据类型
  13. API接口TOKEN设计
  14. 【GIS】GeoHash
  15. Android开发 - 掌握ConstraintLayout(五)偏差(Bias)
  16. Java SE之String,字符串和子字符串的存储与区别
  17. 【Java】 剑指offer(22) 链表中倒数第k个结点
  18. Leetcode 870. 优势洗牌
  19. Eclipse设置自动提示(转)
  20. python list 和 tuple, dict 用于迭代代价

热门文章

  1. c#类中字段和方法中变量的声明问题
  2. spring注入bean的三种方法
  3. 阻止按下backspace键造成页面回退相像
  4. angularjs ng-app=&quot;angular_app&quot; ng-controller=&quot;angular_controller&quot; ng-init=&quot;findAll()&quot;
  5. HTML基础之常用标签
  6. H5输入框在输入信息的时候 页面会变形 并且在页面不变形的时候 键盘会遮挡 输入框的解决办法
  7. stack permutation
  8. 正则表达式-Regular expression学习笔记
  9. 第一课、安装登录CentOS7
  10. 入口文件 index.php 隐藏