Sunday 13 May 2012

UVa 443 Humble Numbers Solution

#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>

using namespace std;
int main()
{

long long a,b,c,d,w,x,y,z,i,j,t,n[10000],e;
memset(n,0,sizeof(n));
a=b=c=d=1;
w=2;
x=3;
y=5;
z=7;
n[1]=1;
t=2;
while(n[6000]==0)
    {
    if(w<x && w<y && w<z) {
                          n[t]=w;
                          a++;
                          w=n[a]*2;
                          t++;
                          }

    else if(x<w && x<y && x<z) {
                          n[t]=x;
                          b++;
                          x=n[b]*3;
                          t++;
                          }

    else if(y<x && y<w && y<z) {
                          n[t]=y;
                          c++;
                          y=n[c]*5;
                          t++;
                          }

    else if(z<x && z<y && z<w) {
                          n[t]=z;
                          d++;
                          z=n[d]*7;
                          t++;
                          }

    if(w==x || w==y) {
             a++;
             w=n[a]*2;
             }
    else if(x==y || x==z) {
             b++;
             x=n[b]*3;
             }
    else if(y==z || y==w) {
             c++;
             y=n[c]*5;
             }
    else if(z==w || z==x) {
             d++;
             z=n[d]*7;
             }
    }
while(cin>>e)
    {
    if(e==0) return 0;
    if(e%100>=10 && e%100<=19) cout<<"The "<<e<<"th humble number is "<<n[e]<<"."<<endl;
    else if(e%10==1) cout<<"The "<<e<<"st humble number is "<<n[e]<<"."<<endl;
    else if(e%10==2) cout<<"The "<<e<<"nd humble number is "<<n[e]<<"."<<endl;
    else if(e%10==3) cout<<"The "<<e<<"rd humble number is "<<n[e]<<"."<<endl;
    else cout<<"The "<<e<<"th humble number is "<<n[e]<<"."<<endl;
    }
return 0;
}

No comments:

Post a Comment