您的位置:首页 > 其它

数学-> YY POJ 1338

2013-10-13 22:07 344 查看
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uSubmit Status Practice POJ1338DescriptionUgly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...shows the first 10 ugly numbers. By convention, 1 is included.Given the integer n,write a program to find and print the n'th ugly number.InputEach line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.OutputFor each line, output the n’th ugly number .:Don’t deal with the line with n=0.Sample Input
1
2
9
0
Sample Output
1
2
10
#include <iostream>using namespace std;int main(){int array[1501] = {1};int a, b, c, n;a = b = c = 0;int temp = 1;for (int i = 1; i < 1501; i++){temp = (2 * array[a] > 3 * array[b] ? 3 * array[b] : 2 * array[a]);temp = (5 * array[c] > temp ? temp : 5 * array[c]);if (temp == 2 * array[a]){a++;}if (temp == 3 * array[b]){b++;}if (temp == 5 * array[c]){c++;}array[i] = temp;}while (cin >> n, n){cout << array[n - 1] << endl;}return 0;}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: