专题三--1004
2016-05-23 22:16
260 查看
题目
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
1234111213212223100100058420
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.
题目大意
humble number是因子有且仅有2,3,5,7四个中至少一个的数(包括1).求第n个humble number。解题思路
本着将问题分解为子问题的思路,可以将每个humble number看作2,3,5,7的相乘组合。即(n为humble number数):n=(2^a)(3^b)(5^c)*(7^d)
确定了a,b,c,d的值即确定了n,先确定好前5842个humble number,存在数组中,直接输出第n个即可
AC代码
#include<iostream> #include<string> #include<stdio.h> #include<cmath> #include<algorithm> using namespace std; long long s[6000] = { 0 }; #define MAX 2000000000 void jisuan(); int main(){ jisuan(); int n; while (cin >> n&&n != 0){ if (n % 10 == 1 && n % 100 != 11) cout << "The " << n << "st" << " humble number is " << s << "." << endl; else if (n % 10 == 2 && n % 100 != 12) cout << "The " << n << "nd" << " humble number is " << s << "." << endl; else if (n % 10 == 3 && n % 100 != 13) cout << "The " << n << "rd" << " humble number is " << s << "." << endl; else cout << "The " << n << "th" << " humble number is " << s << "." << endl; //cout<<"liu"<<endl; } } void jisuan(){ int a, b, c, d; int len = 1; long long num2, num3, num5, num7; for (a = 0; a <= 31; a++) { num2 = (long long)pow(2.0, (double)a); for (b = 0; b <= 20; b++) { num3 = (long long)pow(3.0, (double)b); if (num2*num3 > MAX) break; for (c = 0; c <= 14; c++) { num5 = (long long)pow(5.0, (double)c); if (num2*num3*num5 > MAX) break; for (d = 0; d <= 12; d++) { num7 = (long long)pow(7.0, (double)d); if (num2*num3*num5*num7 <= MAX) s[len++] = num2*num3*num5*num7; else break; } } } } sort(s+1, s+ len); }
相关文章推荐
- 个人报告05
- Java 8新特性——default方法(defender方法)介绍
- centos6.5 搭建hadoop 开发环境(单台服务器)
- 嵌入式DIY电子时钟(有报时和定时)
- [Linux][Install guide]Ubuntu多系统安装注意事项
- 【蓝桥杯】奇怪的数列
- Hbase的API
- spring的定时任务
- Linux I/O模型 与 Java I/O模型、驱动中的异步函数 的区别。
- ./configure: error: C compiler cc is not found 解决方案
- HTML5 WebApp开发(二)表格上拉加载更多下拉刷新
- KMP算法
- linux实践——内核编程 基础模块
- 练习32:列表与for循环
- Poj 3752:走迷宫
- python的socket解决死锁的几个方法
- kalilinux安装lantern
- js underscore常用函数定义及用法
- 聊天程序(C++)
- 无限级分类列表