【题意简述】:就是有这样的一个序列,就拿当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++ 代码 题目本身很简单,注意格式输出!!
#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;
}