uva 136 solution

UVa 136 Ugly Numbers Solution

#include<iostream>

using namespace std;

int main()

{

long n[6000]=

{0},a=1,b=1,c=1,i,j,x=2,y=3,z=5,t=2;

n[1]=1;

while(n[1500]==0)

{

if(x<y && x<z) {

n[t]=x;

a++;

x=n[a]*2;

t++;

}

else if(y<x && y<z) {

n[t]=y;

b++;

y=n[b]*3;

t++;

}

else if(z<y && z<x) {

n[t]=z;

c++;

z=n[c]*5;

t++;

}

if(x==y) {

a++;

x=n[a]*2;

}

else if(y==z) {

b++;

y=n[b]*3;

}

else if(z==x) {

c++;

z=n[c]*5;

}

}

cout<<“The 1500’th ugly number is “<<n

[1500]<<“.”<<endl;

return 0;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s