丑数(humble number) hdu 1058 DP
2013-12-28 23:47
316 查看
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
Write a program to find and print the nth element in this sequence
#include <stdio.h> #define min(a,b) ((a) < (b) ? (a):(b)) #define min4(a,b,c,d) min(min(a,b),min(c,d)) int ans[6000]; void fun() { int a2,a3,a5,a7; int n = 1; ans[1] = 1; a2 = a3 = a5 = a7 = 1; while(ans < 2000000000) { ans[++n] = min4(2*ans[a2],3*ans[a3],5*ans[a5],7*ans[a7]); if(ans == 2*ans[a2]) a2 += 1; if(ans == 3*ans[a3]) a3 += 1; if(ans == 5*ans[a5]) a5 += 1; if(ans == 7*ans[a7]) a7 += 1; } } int main() { int n,ten; fun(); while(~scanf("%d",&n)) { printf("The %d",n); ten = n/10%10; if(n%10 == 1 && ten != 1) printf("st"); else if(n%10 == 2 && ten != 1) printf("nd"); else if(n%10 == 3 && ten != 1) printf("rd"); else printf("th"); printf(" humble number is %d.\n",ans ); } return 0; }
相关文章推荐
- 视频笔记
- Android动画进阶—使用开源动画库nineoldandroids
- java代码--------打印三角形
- JavaScript 正则表达式上——基本语法
- javascript中window.showModalDialog的使用以及windows.open使用整理
- IOS中如何实现对话聊天
- ListBox的虚拟可视化技术
- nginx配置详解(转)
- 我的2013
- Celery 分发任务
- 【Node】node启动过程
- 关于最大连续子序列问题的解法
- 【android,23】手势识别的开发
- Tcp通信
- Hibernate 错误的问题
- 【android,22】屏幕的适配
- 【c++】类中带默认参数的函数
- 369C Valera and Elections
- 创建一门新的编程语言-Flex&Bison&libjit-(5)-流行jit工具之一-libjit
- Extjs报错:isField为空或不是对象