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

Source

University of Ulm Local Contest 1996


思路

丑数的因子只有2,3,5,7,所以可以用从1开始乘这四个因子得到新的数字之后加到数组里面,更新1为2,如此迭代计算下去到算出5842个值,但是这样会存在重复元素问题还要考虑去重问题,数组的元素也不是有序的。

改善这个思路可以有:

设数组a存放所有的丑数,设立4个游标pos分别对应4个因子2,3,5,7,一个数组暂时存储所得的结果。每次都用游标值*4个因子,然后找到最小的值放入数组a,直到数量为5842为止。

注意一个坑:111的英文是one hundred and eleventh,是th结尾的!!!!!!,112,113同理,实际上不止是这三个数,只要符合一定的特征。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int num[6000];
  4. int findMin(int a[],bool f[],int len)
  5. {
  6. int minvalue = a[0];
  7. for(int i=0;i<len;i++)
  8. minvalue = min(minvalue, a[i]);
  9. for(int i=0;i<len;i++)
  10. minvalue == a[i] ? f[i] = true : f[i] = false;
  11. return minvalue;
  12. }
  13. void Init()
  14. {
  15. num[1] = 1;
  16. bool vis[6000];
  17. int tmp[4];
  18. int pos[4] = {1,1,1,1};
  19. int fac[4] = {2,3,5,7};
  20. for(int i=2;i<=5842;i++)
  21. {
  22. for(int j=0;j<4;j++)
  23. tmp[j] = num[pos[j]] * fac[j];
  24. int minvalue = findMin(tmp,vis,4);
  25. num[i] = minvalue;
  26. for(int j=0;j<4;j++)
  27. if(vis[j])
  28. pos[j]++;
  29. }
  30. }
  31. int main()
  32. {
  33. int n;
  34. Init();
  35. while(cin>>n && n!=0)
  36. {
  37. string suffix = "th";
  38. if(n%10==1 && n%100!=11)
  39. suffix = "st";
  40. else if(n%10==2 && n%100!=12)
  41. suffix = "nd";
  42. else if(n%10==3 && n%100!=13)
  43. suffix = "rd";
  44. cout<<"The "<<n<<suffix<<" humble number is "<<num[n]<<"."<<endl;
  45. }
  46. return 0;
  47. }

Hdoj 1058.Humble Numbers 题解的更多相关文章

  1. HDOJ 1058 Humble Numbers(打表过)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  2. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  3. HDU 1058 Humble Numbers (DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. hdu 1058:Humble Numbers(动态规划 DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. HDU 1058 Humble Numbers (动规+寻找丑数问题)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. 【HDOJ】1058 Humble Numbers

    简单题,注意打表,以及输出格式.这里使用了可变参数. #include <stdio.h> #define MAXNUM 5845 #define ANS 2000000000 int b ...

  7. HDU 1058 Humble Numbers(离线打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...

  8. hdu 1058 Humble Numbers

    这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...

  9. HDU 1058 Humble Numbers【DP】

    题意:给出丑数的定义,只含有2,3,5,7这四个素数因子的数称为素数.求第n个丑数. 可以先观察几个丑数得出规律 1:dp[1] 2:min(1*2,1*3,1*5,1*7) 3:min(2*2,1* ...

随机推荐

  1. 周末时间学习Linux

    大家都是如何度过周末时光的呢?好多人都认为一周的工作后要好好休息下,于是在家疯狂的补觉,刷剧,打游戏,自我觉得很是正常,工作几天了,休息下不是当然嘛.是的,休息下很正常,但是把周末的时光都用到这些东西 ...

  2. 微服务治理平台的RPC方案实现

    导读:本文主要探讨了rpc框架在微服务化中所处的位置,需要解决的问题.同时介绍了用友云微服务治理平台的rpc解决方案,为什么选择该方案.该方案提供的好处是什么.同时也会介绍用友RPC框架的基本结构以及 ...

  3. jabRef里引用的相邻同名作者变横线

    用jabRef引用同名作者的文章时,出现了第二个文章的作者变成了横线,在搜了相关资料后,发现作如下修改可避免: 1.在.bib文件中加入开关,并修改默认配置: @IEEEtranBSTCTL{IEEE ...

  4. 牛客OI周赛8-普及组

    https://ac.nowcoder.com/acm/contest/543#question A. 代码: #include <bits/stdc++.h> using namespa ...

  5. docker vm 性能优劣

    Docker容器与虚拟机区别 - unixfbi.com - 博客园 http://www.cnblogs.com/pangguoping/articles/5515286.html docker与虚 ...

  6. js 通过url获取里面的参数值

    场景描述:当我们从一个页面要带有一两个值跳转到另一个页面,另一个页面要使用这些参数的时候,我们就需要通过js获取这些参数啦. 先贴上代码: function getQueryString(name) ...

  7. 配置react-sass

    在配置react-sass时遇到很多坑其中 一条如果你的.scss文件失效 请一定要在fileloader之前配置该sass-loader 配置文件如下 基于你不熟悉webpack 容易出这个错误

  8. 关于spring的源码的理解

    从最基础的Hello World开始. spring的Hello World就三行代码: public void test() { ApplicationContext context = new C ...

  9. AngularJS路由使用案例

    AngularJS路由使用案例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  10. python爬虫之多线程、多进程、GIL锁

    背景: 我们知道多线程要比多进程效率更高,因为线程存在于进程之内,打开一个进程的话,首先需要开辟内存空间,占用内存空间比线程大.这样想也不怪,比如一个进程用10MB,开10个进程就得100MB的内存空 ...