杭电OJ——1058 Humble Numbers(用优先队列也可以解)

Humble Numbers

Description

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 

Write a program to find and print the nth element in this sequence. 

Input

The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.

Sample Input

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

Sample Output

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
发一下AC代码吧!

//采用优先队列应该不会错啊!
#include<iostream>
//#include<queue>
#include<cstdio>
using namespace std;

/*
struct cmp{
       bool operator ()(const int &i,const int &j){
            return i>j;
         }
};
*/

//int prime[4]={2,3,5,7};
 int min(int a,int b,int c,int d)//选出最小值
{
	long long min1=20000000001;
	if(min1>a) min1=a;
    if(min1>b) min1=b;
	if(min1>c) min1=c;
	if(min1>d) min1=d;
	return min1;
}
int main()
{
    int dp[5843];
	int i,n;
	int x1,x2,x3,x4;
	x1=x2=x3=x4=0;//四个指针,开始时指向dp[0]=1;
   // priority_queue<int,vector<int>,cmp> q;//优先队列

    dp[0]=1;
	
    /*
	for(i=1;i<5842;i++)
    {
       for(k=0;k<4;k++)
       q.push(dp[i-1]*prime[k]);
	   while(q.top() <= dp[i-1])//队列输出的值应该比dp[i-1]大!
	   {
		    q.pop();
	   }
	    dp[i]=q.top();
	    q.pop();
     } 
	 */
	//以下参考而来的修改代码!很悲催!我真心觉得优先队列应该不会错啊!可偏偏在dp[4913]这个节点处出问题!
	//按道理说,这是行得通的啊!先把dp[i]的2,3,5,7倍入队列,然后从队列里选一个大于dp[i-1]的最小的数当做dp[i];
	//前4000多个数据都对,偏偏到后头出错!为什么啊?
	//有没有大神可以给我解释一下啊?
	for(i=1;i<5842;i++)//这个参考的算法的确很优!
	{//一个数如果是Humble Numbers,那么它的2,3,5,7倍都是Humble Numbers!
      //x1指示dp[i]的2倍
	  //x2为3倍……
	  //具体结合图看吧!
		 dp[i]=min(dp[x1]*2,dp[x2]*3,dp[x3]*5,dp[x4]*7);
		 if(dp[i]==dp[x1]*2) x1++;//搜到了,指针就往后挪!挪到下一个dp[x1]*2;
		 if(dp[i]==dp[x2]*3) x2++;
         if(dp[i]==dp[x3]*5) x3++;
	     if(dp[i]==dp[x4]*7) x4++;
	}
     while(cin>>n && n != 0)
	{
         if((n%100)/10 !=1 )
		  {
		   if( n%10 == 1)
			printf("The %dst humble number is %d.\n",n,dp[n-1]);
		   else if( n%10 == 2)
		printf("The %dnd humble number is %d.\n",n,dp[n-1]);
		   else if( n%10 == 3)
			printf("The %drd humble number is %d.\n",n,dp[n-1]);
		   else
			printf("The %dth humble number is %d.\n",n,dp[n-1]);
		  }
		  else
		  printf("The %dth humble number is %d.\n",n,dp[n-1]);
		 /* 
         if((n%100)/10 !=1 )
		  {
		   if( n%10 == 1)
			cout<<"The "<<n<<"st humble number is "<<dp[n-1]<<"."<<endl;
		   else if( n%10 == 2)
			cout<<"The "<<n<<"nd humble number is "<<dp[n-1]<<"."<<endl;
		   else if( n%10 == 3)
			cout<<"The "<<n<<"rd humble number is "<<dp[n-1]<<"."<<endl;
		   else
			cout<<"The "<<n<<"th humble number is "<<dp[n-1]<<"."<<endl;
		  }
		  else
		   cout<<"The "<<n<<"th humble number is "<<dp[n-1]<<"."<<endl;
           */
	 }
    system("pause");
    return 0;
}

这道题我还有一些疑问!希望高手解答一下!
 
早些时候,稍稍修改了一下代码!就是这道题用优先队列其实也一样可以解出来!发一下该题的另一种解法吧!
优先队列版:
//采用优先队列应该不会错啊!
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;

struct cmp{
       bool operator ()(const _int64 &i,const _int64 &j){
            return i>j;
         }
};


int prime[4]={2,3,5,7};

int main()
{
    _int64 dp[5843];
	int i,n;
	int k;
   priority_queue<_int64,vector<_int64>,cmp> q;//优先队列

    dp[0]=1;
	
	for(i=1;i<5842;i++)
    {
       for(k=0;k<4;k++)
       q.push(dp[i-1]*prime[k]);
	   while(q.top()==dp[i-1])//刚开始的时候我是写的 q.top()<=dp[i-1] ,不过按这样来写的话,dp[i]当i到了4000多的时候就会出错!
	   //队列输出的值应该比dp[i-1]大!
	   {//先解释一下写 q.top()==dp[i-1]的原因吧!由于 dp[i-1]=优先队列里的最小值 ,现在dp[i-1]的2,3,5,7倍入队列,也就是说
		//现在队列里的值都大于等于dp[i-1],由于该种数不允许重复,故一旦q.top()==dp[i-1],就出队列!
		//我之前写的 q.top()<=dp[i-1]的<有些多余!
		//不过至于为什么会出错,我也不太清楚!
		    q.pop();
	   }
	    dp[i]=q.top();
	    q.pop();
     } 
	//先把dp[i]的2,3,5,7倍入队列,然后从队列里选一个大于dp[i-1]的最小的数当做dp[i];
     while(cin>>n && n != 0)
	{
         if((n%100)/10 !=1 )
		  {
		   if( n%10 == 1)
			printf("The %dst humble number is %d.\n",n,dp[n-1]);
		   else if( n%10 == 2)
		printf("The %dnd humble number is %d.\n",n,dp[n-1]);
		   else if( n%10 == 3)
			printf("The %drd humble number is %d.\n",n,dp[n-1]);
		   else
			printf("The %dth humble number is %d.\n",n,dp[n-1]);
		  }
		  else
		  printf("The %dth humble number is %d.\n",n,dp[n-1]);
	 }
    system("pause");
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值