Re: 136 Ugly number
Posted: Tue Oct 01, 2013 11:55 pm
Print a newline char at the end of the last line.
Code: Select all
#include <iostream>
using namespace std;
int main() {
cout.sync_with_stdio( false );
cout << "The 1500'th ugly number is 859963392.\n";
return 0;
}
Code: Select all
int main() {return 0;}
Code: Select all
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;
long ugly[1500];
int minimum(const int& b2,const int& b3,const int& b5)
{
int a = ugly[b2-1]*2;
int b = ugly[b3-1]*3;
int c = ugly[b5-1]*5;
if((a<b)&&(a<c)) return 1;
else if((b<a)&&(b<c)) return 2;
else if((c<a)&&(c<b))return 3;
else if((a==b)&&(b==c)) return 7;
else if(a==b) return 4;
else if(b==c) return 5;
else if(a==c) return 6;
}
int update_next_ug(int& b2, int& b3, int& b5, int &ug_num)
{
int condition = minimum(b2,b3,b5);
++ug_num;
int next_ug = 1;
switch(condition)
{
case 1:
next_ug = ugly[b2-1]*2;
++b2;
break;
case 2:
next_ug = ugly[b3-1]*3;
++b3;
break;
case 3:
next_ug = ugly[b5-1]*5;
++b5;
break;
case 4:
next_ug = ugly[b2-1]*2;
++b2;
++b3;
break;
case 5:
next_ug = ugly[b3-1]*3;
++b3;
++b5;
break;
case 6:
next_ug = ugly[b2-1]*2;
++b2;
++b5;
break;
case 7:
next_ug = ugly[b2-1]*2;
++b2;
++b5;
++b3;
break;
}
return next_ug;
}
int main(void)
{
int b2 = 1;
int b3 = 1;
int b5 = 1;
int ug_num = 1;
long last_ug = 1;
while(1)
{
ugly[ug_num-1] = last_ug;
if(ug_num==1500) break;
last_ug = update_next_ug(b2,b3,b5,ug_num);
}
printf("The 1500'th ugly number is %d.",ugly[1499]);
return 0;
}