丑数

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值