Humble Numbers

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

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=∏q^r即由素数之积构成....
代码:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*策略打表实现存储*/
__int64 sav[];
int cmp(void const *a,void const *b)
{
return (*(__int64 *)a)-(*(__int64 *)b);
}
int work()
{
__int64 a,b,c,d,aa,bb,cc,dd;
int cnt=;
for(a=,aa=; ;a++)
{
if(a!=) aa*=;
if(aa>) break;
for(b=,bb=; ; b++)
{
if(b!=) bb*=;
if((aa*bb)>) break;
for(c=,cc=; ; c++)
{
if(c!=)cc*=;
if((aa*bb*cc)>) break;
for(d=,dd=; ;d++)
{
if(d!=) dd*=;
if((aa*bb*cc*dd)>) break;
sav[cnt++]=(aa*bb*cc*dd);
}
}
}
} return cnt;
}
int main()
{
int n;
qsort(sav,work(),sizeof(sav[]),cmp);
while(scanf("%d",&n),n)
{
int tem=n%;
printf("The ");
if(tem>&&tem<)
printf("%dth ",n);
else
{
tem%=;
if(tem==)
printf("%dst ",n);
else if(tem==)
printf("%dnd ",n);
else if(tem==)
printf("%drd ",n);
else
printf("%dth ",n);
}
printf("humble number is %I64d.\n",sav[n-]);
}
return ;
}

最新文章

  1. NewQuant的设计(一)——整体的领域设计
  2. android unique identifier
  3. 引擎设计跟踪(九.14.2e) DelayLoaded DLLs (/DELAYLOAD)
  4. 1154 能量项链[区间dp]
  5. iOS-default.png启动图片
  6. sdut 1570 c旅行
  7. FloatingActionButton
  8. .NET+Oracle 分页
  9. hdu-1010 dfs+剪枝
  10. Stm32高级定时器(三)
  11. c++,派生类对象可以对基类赋值,基类对派生类不可以赋值
  12. 多字符集(ANSI)和UNICODE及字符串处理方式准则
  13. 解决ssh或ftp下root用户认证失败问题
  14. Redis中的数据结构与常用命令
  15. JDBC各种数据库连接方式
  16. ES--07
  17. Windows邮件客户端
  18. 强迫症犯了,忍不住赞一下slf4j包Logger.java的优雅代码
  19. Velocity.js初识
  20. English trip M1 - PC12 I&#39;d Like a Room Please Teacher:Taalan

热门文章

  1. 推荐Java程序员阅读的书籍(转)
  2. AngularJs 阻止事件运行,防止冒泡穿透事件
  3. 十个WEB开发人员不可不知的HTML5工具
  4. Linq-分页查询
  5. telnet 退出命令
  6. Cognos事件工作室Event Studio开发步骤
  7. IE11与Cognos怪谈
  8. ORACLE关于锁表查询的部分SQL
  9. 如何使用千千静听为MP3添加专辑封面和文字信息
  10. Linux学习笔记--which命令(搜索命令的命令)