这个和上一道HDU 3199 Hamming Problem是类似的,有了思路就开始码代码了,可是!序数词的写法不对(代码注释部分)又上网普及了序数词的写法···
搜到其他解题报告 其中有把这道题分类为DP的,最优子结构?无后效性?
还有“寻找丑数问题” 详情点这里。
#include<iostream> #include<cstdio> using namespace std; const int Size=5842; long long num[Size+1]; int main() { int k1, k2, k3, k4, n; k1=k2=k3=k4=1; num[1]=1; for(int i=2; i<Size+1; i++) { long long x1=num[k1]*2; long long x2=num[k2]*3; long long x3=num[k3]*5; long long x4=num[k4]*7; long long M1=min(x1, x2); long long M2=min(x3, x4); M1=min(M1, M2); num[i]=M1; if(M1==x1) k1++; if(M1==x2) k2++; if(M1==x3) k3++; if(M1==x4) k4++; } while(cin>>n&&n!=0) { // int t=n%10; // if(n==11 || n==12 || n==13) // t=n; if(n % 10 == 1 && n % 100 != 11) cout<<"The "<<n<<"st humble number is "<<num[n]<<"."<<endl; else if(n % 10 == 2 && n % 100 != 12) cout<<"The "<<n<<"nd humble number is "<<num[n]<<"."<<endl; else if(n % 10 == 3 && n % 100 != 13) cout<<"The "<<n<<"rd humble number is "<<num[n]<<"."<<endl; else cout<<"The "<<n<<"th humble number is "<<num[n]<<"."<<endl; // switch(t) // { // case 1: cout<<"The "<<n<<"st humble number is "<<num[n]<<"."<<endl; break; // case 2: cout<<"The "<<n<<"nd humble number is "<<num[n]<<"."<<endl; break; // case 3: cout<<"The "<<n<<"rd humble number is "<<num[n]<<"."<<endl; break; // default:cout<<"The "<<n<<"th humble number is "<<num[n]<<"."<<endl; break; // } } return 0; }
时间: 2024-10-08 05:01:18