HDOJ1058(Humble Numbers)dp
#include <iostream>
using namespace std;
const int N = 5850;
__int64 humble[N] = {0, 1};
void makeHumbleNum()
{
int p2, p3, p5, p7;
__int64 minv;
p2 = p3 = p5 = p7 = 1;
for(int i = 2; i <= 5842; i++)
{
minv = humble[p2] * 2;
if(humble[p3] * 3 < minv) minv = humble[p3] * 3;
if(humble[p5] * 5 < minv) minv = humble[p5] * 5;
if(humble[p7] * 7 < minv) minv = humble[p7] * 7;
//-----------------------------------------------
if(minv == humble[p2] * 2) p2++;
if(minv == humble[p3] * 3) p3++;
if(minv == humble[p5] * 5) p5++;
if(minv == humble[p7] * 7) p7++;
humble[i] = minv;
}
}
int main()
{
int n;
makeHumbleNum();
while(cin>>n && n)
{
cout<<"The "<<n;
if(n==11 || n==12 || n==13)
cout<<"th";
else if( n%10==1 && n%100 !=11)//注意n%100 !=11这个条件
cout<<"st";
else if( n%10==2 && n%100 !=12)
cout<<"nd";
else if( n%10==3 && n%100 !=13)
cout<<"rd";
else
cout<<"th";
printf(" humble number is %I64d.\n",humble[n]);
}
return 0;
}
using namespace std;
const int N = 5850;
__int64 humble[N] = {0, 1};
void makeHumbleNum()
{
int p2, p3, p5, p7;
__int64 minv;
p2 = p3 = p5 = p7 = 1;
for(int i = 2; i <= 5842; i++)
{
minv = humble[p2] * 2;
if(humble[p3] * 3 < minv) minv = humble[p3] * 3;
if(humble[p5] * 5 < minv) minv = humble[p5] * 5;
if(humble[p7] * 7 < minv) minv = humble[p7] * 7;
//-----------------------------------------------
if(minv == humble[p2] * 2) p2++;
if(minv == humble[p3] * 3) p3++;
if(minv == humble[p5] * 5) p5++;
if(minv == humble[p7] * 7) p7++;
humble[i] = minv;
}
}
int main()
{
int n;
makeHumbleNum();
while(cin>>n && n)
{
cout<<"The "<<n;
if(n==11 || n==12 || n==13)
cout<<"th";
else if( n%10==1 && n%100 !=11)//注意n%100 !=11这个条件
cout<<"st";
else if( n%10==2 && n%100 !=12)
cout<<"nd";
else if( n%10==3 && n%100 !=13)
cout<<"rd";
else
cout<<"th";
printf(" humble number is %I64d.\n",humble[n]);
}
return 0;
}