HDU 1058 Humble Numbers

Source: Internet
Author: User

Humble Numbers

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18406 Accepted Submission (s): 8010


Problem Descriptiona number whose only prime factors be 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, ten,, +, (+), A, ten, A, ten, ten, ... shows the first humble numbers.

Write a program to find and print the nth element in this sequence

Inputthe input consists of one or more test cases. Each test case consists the one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for N.

Outputfor each test case, print one line saying "The nth humble number is number." Depending on the value of n, the correct suffix "st", "nd", "ts", or "th" for the ordinal number nth have to is used like I T is shown in the sample output.

Sample INPUT1 2 3 4 11 12 13 21 22 23 100 1000 5842 0

Sample outputthe 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. The first time to do this is a simple simulation, but a look at 2000000000 definitely timed out, and later looked at what others do to know.
#include <iostream>using namespace Std;int min (int a,int b,int c,int d) {int t=a;    if (b<a) a=b;    if (c<a) a=c;    if (d<a) A=d; return A;}    int main () {int s2,m,s3,s5,s7,n,a[6000];          A[1]=1;          S2=s3=s5=s7=1;          M=1;                while (a[m]<=2000000000) {m++;                A[m]=min (2*a[s2],3*a[s3],5*a[s5],7*a[s7]);                if (A[m]==2*a[s2]) s2++;                if (A[M]==5*A[S5]) s5++;                if (A[M]==3*A[S3]) s3++;          if (A[m]==7*a[s7]) s7++;          } while (Cin>>n) {if (n==0) break;          cout << "the" <<n;          int t=n%10;          int tt=n%100;          if (t==1&&tt!=11) cout << "St";          else if (t==2&&tt!=12) cout << "nd";          else if (t==3&&tt!=13) cout << "RD";          else cout << "th"; cout << "Humble number is" <<a[n]<< "."    <<endl; } return 0;}

HDU 1058 Humble Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.