练习三 1004
2016-05-03 16:23
281 查看
概述:有一个数列,它的成员都是2,3,5,7的倍数,输入一个n,求出这个数列的第n个数。
思路:先列一个表,把这些数据全求出来,利用循环,设a,b,c,d=1,分别对他们*2,*3,*5,*7,取最小值入表,并将其+1,依次类推,最后输出所求值即可。
感想:以前做过的题,拿来想想就有思路了。
思路:先列一个表,把这些数据全求出来,利用循环,设a,b,c,d=1,分别对他们*2,*3,*5,*7,取最小值入表,并将其+1,依次类推,最后输出所求值即可。
感想:以前做过的题,拿来想想就有思路了。
#include <iostream> #include <cstdio> using namespace std; typedef long long LL; LL dp[5843]; LL min(LL a, LL b, LL c, LL d) { LL temp1 = a < b ? a : b; temp1 = temp1 < c ? temp1 : c; temp1 = temp1 < d ? temp1 : d; return temp1; } int main() { dp[1] = 1; int a = 1, b = 1, c = 1, d = 1; for (int i = 2; i <= 5842; ++i) { dp[i] = min(dp[a] * 2, dp[b] * 3, dp[c] * 5, dp[d] * 7); if (dp[i] == dp[a] * 2) a++; if (dp[i] == dp[b] * 3) b++; if (dp[i] == dp[c] * 5) c++; if (dp[i] == dp[d] * 7) d++; } int n; while (cin>>n&&n) { if (n % 100 != 11 && n % 10 == 1)printf("The %dst humble number is %lld.\n", n, dp ); else if (n % 100 != 12 && n % 10 == 2)printf("The %dnd humble number is %lld.\n", n, dp ); else if (n % 100 != 13 && n % 10 == 3)printf("The %drd humble number is %lld.\n", n, dp ); else printf("The %dth humble number is %lld.\n", n, dp ); } return 0; }
相关文章推荐
- 【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
- html5下让 height:100%; 起作用
- 计算机应用与软件投稿图片要黑白的
- oracle基本概念
- 腾讯云大数据助力智慧城市发展
- mysql 悲观锁 的运用
- UVA 10474 Where is the Marble? 暴力|STL
- Build sample addon in android5.1.1
- "无法安装64位版本的Office,因为在您的PC上找到了以下32位程序"的解决办法
- nyoj_18 The Triangle
- Sublime Text3 支持Less
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- python中os.walk浏览目录和文件
- 邮件重新引发“林来疯”时刻?
- 堆排序讲解及Java实现
- 数据库笔记
- java 实现大数的阶乘
- ScrollView实现类似QQ可拉动
- chmod u+s(转)
- JAVA之StringBuffer测试样码