3 solutions

  • 1
    @ 2022-5-2 15:42:03

    哈希表+优先队列

    耗时较高

    #include<bits/stdc++.h>
    using namespace std;
    int a,n;
    int main(){
    	while(scanf("%d%d",&a,&n)!=EOF){
    		unordered_map<int,int>ma;
    		int ans,cnt=0;
    		priority_queue<int,vector<int>,greater<int> >q;
    		q.push(a);
    		while(cnt!=n){
    			if(ma[q.top()]){
    				q.pop();
    				continue;
    			}
    			ans=q.top();
    			ma[ans]=1;
    			q.pop();
    			cnt++;
    			q.push(2*ans+1),q.push(3*ans+1);
    		}
    		printf("%d\n",ans);
    	}
    	return 0;
    }
    
    • 0
      @ 2022-3-31 19:18:21
      #include<bits/stdc++.h>
      using namespace std;
      int x,n;
      int main(){
      	std::ios::sync_with_stdio(false);
      	while(cin>>x>>n){
      		queue <int> num,num2,num3;
      		num.push(x);
      		int i=1;
      		for(;i<n;i++){//
      			num2.push(num.back()*2+1);
      			num3.push(num.back()*3+1);
      			if(num2.front()<num3.front()){
      				num.push(num2.front());
      				num2.pop();
      			}else if(num2.front()>num3.front()){
      				num.push(num3.front());
      				num3.pop();
      			}else{
      				num.push(num3.front());
      				num3.pop();
      				num2.pop();
      			}
      		}
      		cout<<num.back()<<endl;
      	}
      	return 0;
      }
      
      • 0
        @ 2022-3-30 11:40:48

        1333:【例2-2】Blah数集 一本通 队列_Star77777的博客-CSDN博客_一本通1333

        #include <bits/stdc++.h>
        using namespace std;
        
        int main(){
            int a,n,count = 1,x;
            while(cin>>a>>n){
                x = a,count = 1;//**重置x和count**
                queue<int> q1,q2;//一个队放2x+1,一个队放3x+1,每次循环自动清空
                while(count < n){
                    q1.push(x * 2 + 1);
                    q2.push(x * 3 + 1);
                    if(q1.front() > q2.front()){
                        x = q2.front();
                        q2.pop();
                    }
                    else if(q1.front() < q2.front()){
                        x = q1.front();
                        q1.pop();
                    }
                    else{//一旦出现一个重复,如果2个队首都不删除的话,之后都会出现重复的
                        x = q2.front();
                        q1.pop();
                        q2.pop();
                    }
                    count++;
                }
                cout<<x<<endl;
            }
            return 0;
        }
        
        • 1

        Information

        ID
        297
        Time
        1000ms
        Memory
        128MiB
        Difficulty
        5
        Tags
        # Submissions
        23
        Accepted
        12
        Uploaded By