POJ 2545+2591+2247+1338簡單水題
阿新 • • 發佈:2017-05-17
變形 humble long 列數 最簡 能夠 poj string num
另外三道題與這個相差無幾,都是這個的變形
【題意簡述】:就是有這種一個序列。就拿當p1 = 2,p2 = 3, p3 = 5,來舉例。由這三個數為基準組成的序列是:
2,3,4,5,6,8,9,10,12……如今給你這個序列數組的下標,讓你求得這個數組中,這個下標裏的數是多少。
【分析】:2,3,4,5,6,8,9,10,12……這個序列式由2,3,5這三個數生成的,詳細怎樣生成,就是:
詳見代碼:
這裏以POJ2545為例:
//弄清當中的邏輯關系,能夠從最簡單的2,3,5試著做起! #include<iostream> #include<algorithm> using namespace std; int main() { long long p1,p2,p3; int n; long long H[100005]; int a1,a2,a3; while(cin>>p1>>p2>>p3>>n) { H[1] = 1; a1=a2=a3=1; for(int i = 2;i<=n+1;i++) { H[i] = min(p1*H[a1],min(p2*H[a2],p3*H[a3])); if(H[i]==p1*H[a1]) a1++; if(H[i]==p2*H[a2]) a2++; if(H[i]==p3*H[a3]) a3++; } cout<<H[n+1]<<endl; } return 0; }
另外三道題與這個相差無幾,都是這個的變形
僅僅只是2247 註意輸出的格式!
// C++ 代碼 題目本身非常easy。註意格式輸出。! #include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { int a1,a2,a3,a4; int n; long long humble[6000]; humble[1]=1; a1=1; a2=1; a3=1; a4=1; for(int i=2;i<=5842;i++) { humble[i] = min(2*humble[a1],min(3*humble[a2],min(5*humble[a3],7*humble[a4]))); if(humble[i]==2*humble[a1]) a1++; if(humble[i]==3*humble[a2]) a2++; if(humble[i]==5*humble[a3]) a3++; if(humble[i]==7*humble[a4]) a4++; } //string b; while(1) { cin>>n; if(n==0) break; if(n%10==1) { if(n%100==11) cout<<"The "<<n<<"th"<<" humble number is "<<humble[n]<<"."<<endl; else cout<<"The "<<n<<"st"<<" humble number is "<<humble[n]<<"."<<endl; } if(n%10==2) { if(n%100==12) cout<<"The "<<n<<"th"<<" humble number is "<<humble[n]<<"."<<endl; else cout<<"The "<<n<<"nd"<<" humble number is "<<humble[n]<<"."<<endl; } if(n%10==3) { if(n%100==13) cout<<"The "<<n<<"th"<<" humble number is "<<humble[n]<<"."<<endl; else cout<<"The "<<n<<"rd"<<" humble number is "<<humble[n]<<"."<<endl; } if(n%10>3||n%10==0) cout<<"The "<<n<<"th"<<" humble number is "<<humble[n]<<"."<<endl; } }
POJ 2591
// 39392K 141Ms // 打表過得 =_= #include<iostream> #include<algorithm> using namespace std; int S[10000001]; int main() { int a,b,n; S[1] = 1; a = b = 1; for(int i = 2;i<=10000000;i++) { S[i] = min(2*S[a]+1,3*S[b]+1); if(S[i] == 2*S[a]+1) a++; if(S[i] == 3*S[b]+1) b++; } while(cin>>n) { cout<<S[n]<<endl; } return 0; }
POJ 2545+2591+2247+1338簡單水題