Ugly Numbers
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72
题意:
求以2,3,5为唯一因子的第1500个数字(丑数)
AC代码:
#include<iostream> using namespace std; int minu(int a,int b,int c) { int temp; temp=a>b?b:a; return temp>c?c:temp; } int main() { int ugly[1505]; ugly[0]=1; int ugly2=0,ugly3=0,ugly5=0; for (int i=1;i<1500;i++) { int mini=minu(ugly[ugly2]*2,ugly[ugly3]*3,ugly[ugly5]*5); if (mini==ugly[ugly2]*2) ugly2++; if (mini==ugly[ugly3]*3) //不要写成else if,因为有可能最小值与两个值相等 ugly3++; if (mini==ugly[ugly5]*5) //同上 ugly5++; ugly[i]=mini; } cout << "The 1500'th ugly number is "; cout << ugly[1500-1] << "." << endl; return 0; }