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;
}
思路:因为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;
}
相关文章推荐
- HDU 1058 Humble Numbers
- 【HDU】1058 - Humble Numbers(dp)
- hdu 1058 Humble Numbers
- HDU 1058Humble Numbers(dp)
- hdu 1058 Humble Numbers【dp】
- HDU 1058 Humble Numbers--DP--(计数器技巧)
- hdu 1058:Humble Numbers(动态规划 DP)
- HDU 1058 Humble Numbers
- HDU 1058 Humble Numbers
- hdu 1058 Humble Numbers【丑数】
- HDOJ(HDU).1058 Humble Numbers (DP)
- HDU 1058(Humble Numbers)动态规划
- hdu 1058 Humble Numbers
- DP Problem D:Humble Numbers(HDU 1058)
- HDU 1058 Humble Numbers【DP】
- HDU 丑数 - 1058 Humble Numbers
- Hdu 1058 Humble Numbers
- HDU 1058 Humble Numbers (dp+打表)
- hdu 1058 Humble Numbers(dp)
- HDU:1058 Humble Numbers(技巧打表)