# 丑数

#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
vector<long long int>  GetUglyNumber_Solution(int index) {
int t, i;
vector<long long int> ver;
vector<long long int> ver1;
if (index <7)
{
for (int i = 0; i <= index; i++)

{
//cout << i<<" ";
ver.push_back(i);
}

}
//cout << index;

if (index >=7)
{
for (int i = 0; i <= 6; i++)

{
//cout << i<<" ";
ver.push_back(i);
}
}

for (i = 7; i <= index; ++i)
{
t = i;
while (t%3==0)
{
t = t / 3;

if (t==3||t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}

}

for (i = 7; i <= index; ++i)
{
t = i;
while (t % 2 == 0)
{
t = t / 2;

if (t == 3 || t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}

}
for (i = 7; i <= index; ++i)
{
t = i;
while (t %5 == 0)

{
t = t / 5;

if (t == 3 || t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}

}

for (i = 7; i <= index; ++i)
{
t = i;
while (t % 10 == 0)
{
t = t / 10;

if (t == 3 || t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}
}

for (i = 7; i <= index; ++i)
{
t = i;
while (t % 6 == 0)

{
t = t / 6;

if (t == 3 || t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}

}

for (i = 7; i <= index; ++i)
{
t = i;
while (t %15 == 0)
{
t = t / 15;

if (t == 3 || t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}

}

for (i = 7; i <= index; ++i)
{
t = i;
while (t % 30 == 0)

{
t = t / 30;

if (t == 3 || t == 2 || t == 5)
{
//cout << i << " ";
ver.push_back(i);
break;
}
}

}
cout << endl;
for (i = 1; i < ver.size(); i++)//直接从1开始
{
int n = 0;
for (int j = 0; j<i; j++)

if (ver[i] != ver[j])
{
n++;
if (n == i)
{
//cout << ver[i]<<" ";
ver1.push_back(ver[i]);
//n = 0;
int p = n;
}
}
}

cout << endl;
sort(ver1.begin(), ver1.end()); //从大到小排序

/*int  j, temp;  //c语言方法实现从大到小排序
for (i = 0; i<ver1.size(); i++)
for (j = i; j<ver1.size(); j++)
if (ver1[i]>ver1[j])
{
temp = ver1[i];
ver1[i] = ver1[j];
ver1[j] = temp;
}
*/

return ver1;

}

int main()
{
//int* a =GetUglyNumber_Solution;
int a[1500];
vector<long long int> v(GetUglyNumber_Solution(1500));

for (int i = 0; i < v.size(); i++)//直接从1开始
{
cout<<v[i]<<" ";
//cout << GetUglyNumber_Solution(9)[i];//虽然正确。
//但直接输出换很多空行
//printf("%d", a[1]) ;

}
//OutputVector(GetUglyNumber_Solution(72);
cout << endl;
return 0;
}

04-02 1506

04-23 3904

03-24 2813

07-26 1831

03-27 2176

08-19 674

10-27 503

#### 求出第1500个的丑数是多少

2011年12月12日 357B 下载

05-30 5275

03-27 1107