您的位置:首页 > 其它

容器set的应用:丑数 UVA - 136

2017-02-09 21:58 309 查看
只有因子为2,3,5的正整数为丑数。

output

将下面输出示例中的number替换为第一千五百个丑数。

sample output

The 1500’th ugly number is number.

代码概要

根据已知的丑数不停的生成新的丑数,添加到集合s中(利用了集合中的元素不重复并且按从小到达排序的特点),取生成的第一千五百个丑数,停止生成并输出。

代码

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
set<int> s;
int main(){
s.insert(1);
int count=1;
for(set<int>::iterator i=s.begin();i!=s.end();i++)
if(count==1500){
printf("The 1500'th ugly number is %d.\n",*i);
break;}
else {
count++;
s.insert((*i)*2);
s.insert((*i)*3);
s.insert((*i)*5);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: