题意:输出第n个能拆分成由,2,3,5,7中的一个或者多个组成的数;
解题思路:a[i+1]=a[i]*2;a[i+2]=a[i]*3;a[i+3]=a[i]*5;a[i+4]=a[i]*7;然后把重复的去掉就行了,可以用map标记,也可以用set存,看心情;注意1是第一个数,我也不知道为什么0.0;
#include<iostream> #include<algorithm> #include<map> #include<cstdio> #define ll long long #define maxn 1000000000 using namespace std; ll a[60000]; map<ll,int>m; int cnt=0; int main() { int z; int n; a[++cnt]=1; for(int i=1;i<=10000;i++) { if(a[i]>2*maxn) continue; if(m[a[i]*2]==0&&a[i]<=maxn) { a[++cnt]=a[i]*2; m[a[i]*2]=1; } if(m[a[i]*3]==0&&(a[i]*3)<=(maxn*2)) { a[++cnt]=a[i]*3; m[a[i]*3]=1; } if(m[a[i]*5]==0&&(a[i]*3)<=(maxn*2)) { a[++cnt]=a[i]*5; m[a[i]*5]=1; } if(m[a[i]*7]==0&&(a[i]*3)<=(maxn*2)) { a[++cnt]=a[i]*7; m[a[i]*7]=1; } } sort(a+1,a+1+cnt); while(scanf("%d",&n)&&n){ if(n%100==11 || n%100==12 || n%100==13) printf("The %dth humble number is %lld.\n",n,a[n+1]); else if(n%10==1) printf("The %dst humble number is %lld.\n",n,a[n+1]); else if(n%10==2) printf("The %dnd humble number is %lld.\n",n,a[n+1]); else if(n%10==3) printf("The %drd humble number is %lld.\n",n,a[n+1]); else printf("The %dth humble number is %lld.\n",n,a[n+1]); } return 0; }