您的位置:首页 > 其它

hdu 1058 Humble Numbers (set)

2014-03-30 11:47 459 查看
小记:没做之前,一实验室的学弟问了我,他用的是set,然后前面的数据基本上全对了,就是后面的answer不对。我看了一下他的代码,发现没什么问题,一下子也没想出来。然后刚刚自己试了一下,稍微将赋值操作简化了下,结果就全对了,之前还一直以为是set的问题,看来set的用法是必须注意,一个不小心就完成不了自己想要的结果。

思路:因为set是有序的且里面的元素不重复,使用的是RB树,所以利用这个性质,乘一个2||3||5||7,然后插入set,它自动会排好序,然后调下一个,继续乘,然后插。直到第5842个数。 将这个5842个数存放到数组里去。问一个输出一个即可。输出在1 2 3 和11 12 13 这里要特殊处理下。

代码:

#include <stdio.h>
#include <string.h>
#include <stack>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

const int MAX_ = 5900;

long long h[MAX_];
int next[]={2,3,5,7};

set<long long>q;
set<long long>::iterator p;

void init(){
q.insert(1);
p = q.begin();
int cnt = 0;
while(cnt < 5843){
for(int i = 0; i < 4; ++i){
q.insert((*p) * next[i]);
}
h[cnt++] = *p;
p++;
}
}

int main() {
long long maxm,k;
int n;
init();
while(scanf("%d",&n),n){
switch(n%10){
case 1:
if(n % 100 == 11){
printf("The %dth humble number is %I64d.\n",n,h[n-1]);break;
}else
printf("The %dst humble number is %I64d.\n",n,h[n-1]);break;
case 2:
if(n % 100 == 12){
printf("The %dth humble number is %I64d.\n",n,h[n-1]);break;
}else
printf("The %dnd humble number is %I64d.\n",n,h[n-1]);break;
case 3:
if(n % 100 == 13){
printf("The %dth humble number is %I64d.\n",n,h[n-1]);break;
}else
printf("The %drd humble number is %I64d.\n",n,h[n-1]);break;
default :
printf("The %dth humble number is %I64d.\n",n,h[n-1]);break;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: