# 丑数

190人阅读 评论(0)

#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;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：68043次
• 积分：2971
• 等级：
• 排名：第12262名
• 原创：210篇
• 转载：128篇
• 译文：0篇
• 评论：19条
最新评论