时间限制:2000MS 代码长度限制:10KB
提交次数:0 通过次数:0
题型: 编程题 语言: G++;GCC
Description
Ugly 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. Then, here are the first 10 Not ugly numbers:7, 11, 13, 14, 17, 19,
21, 22, 23, 26. Given the integer n, write a program to find and print the n’th Not ugly number.
输入格式
First line is T(T<=10000), the number of cases.
The T lines following. Each line of the input contains a positive integer n (n <= 100000000).
输出格式
For each case, output the n’th Not ugly number .
输入样例
3
1
2
9
输出样例
7
11
23
#include<iostream>
#include<stdio.h>
int a[1200];
using namespace std;
int ugly(int n)
{
while (n % 2 == 0)
n = n / 2;
while (n % 3 == 0)
n = n / 3;
while (n % 5 == 0)
n = n / 5;
if (n == 1)
return 1;
else
return 0;
}
int main()
{
int i;
int x = 0;
for (i = 1; i <= 100000000; i++)
{
if (ugly(i))
{
a[x] = i;
x++;
}
}
int n;
cin >> n;
int count = 0;
while (n--)
{
int m;
cin >> m;
for (i = 0; i <= 1200; i++)
{
count += a[i + 1] - a[i] - 1;
if (count >= m)
break;
}
cout << a[i + 1] + m - count - 1 << endl;
count = 0;
}
}