丑数humble number
2015-11-06 20:24
190 查看
Humble Numbers
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 30 Accepted Submission(s) : 13
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
Source
ACcode 1:#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
long long dp[5842];
int main()
{
int n;
dp[1]=1;
int i=2;
int two=1,three=1,five=1,seven=1;
while(i<=5842)
{
dp[i]=min(min(min(2*dp[two],3*dp[three]),5*dp[five]),7*dp[seven]);
if(dp[i]==2*dp[two])two++;
if(dp[i]==3*dp[three])three++;
if(dp[i]==5*dp[five])five++;
if(dp[i]==7*dp[seven])seven++;
i++;
}
while(scanf("%d",&n)&&n)
{
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %I64d\n",n,dp
);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %I64d\n",n,dp
);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %I64d\n",n,dp
);
else
printf("The %dth humble number is %I64d\n",n,dp
);
}
}
Accode 2:
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
long long dp[5842];
int main()
{
dp[1]=1;
int prime[4]= {2,3,5,7};
for(int i=2; i<=5842; i++)
{
dp[i]=2000000001;
for(int k=0; k<4; k++)
for(int j=i-1; j>=1; j--)
{
//if(dp[i-1]>=dp[j]*prime[k])
// break;
//if (dp[i]>dp[j]*prime[k])
// dp[i]=dp[j]*prime[k];
if(dp[i-1]<dp[j]*prime[k]&&dp[i]>dp[j]*prime[k])
dp[i]=dp[j]*prime[k];
}
}
int n;
while(scanf("%d",&n)&&n)
{
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %I64d\n",n,dp
);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %I64d\n",n,dp
);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %I64d\n",n,dp
);
else
printf("The %dth humble number is %I64d\n",n,dp
);
}
return 0;
}
相关文章推荐
- leetcode-Summary Ranges
- Jquery常用的功能
- Android TextView中插入图片
- 《剑指offer》——矩形覆盖
- 无法定位程序输入点 @Idwinsock@initialization$qqrv 于动态链接库 indy60.bpl 上。
- 基数排序
- StarUML官网地址 http://staruml.io/
- 统计给定整数的二进制表示中1的个数
- 软件功能测试工具介绍
- 远端绑定Service第二篇之服务器端的Service
- Unity行为树
- 关于count()函数一些问题
- LeetCode:Single Number III
- 新人初来乍到
- 团队,不只是团队
- HDU 4503 数学
- UML总结之用例图
- 判断是否为邮箱,电话的匹配规则
- 《王爽——汇编语言》P172答案
- 欢迎使用CSDN-markdown编辑器