这一题我本来想用链表做的 但是出错了 最终只好用队列模拟 求大佬给一个链表题解

下方是我的题解(仅供参考)

#include<iostream>
#include<queue>

using namespace std;

queue <int> child;

int cnt = 1;

int main()
{
    
    int a,n;
    cin >> a >> n;
    
    for(int i = 1; i <= a; i++){
        
        child.push(i);
    }
    
    while(child.empty() == false){
        
        for(int i = 0; i < child.size(); i++){
            
            if(cnt % n == 0){
                
                cout << child.front() << " ";
                
                child.pop();
            }
            
            else{
            	
            	child.push(child.front());
            	
            	child.pop();
			}
            
            cnt++;
        }
    }
    
    return 0;
}

1 comments

  • @ 2024-9-14 21:52:48

    求求了 给个链表题解吧 我现在队列、栈、图、链表中就链表没掌握

    • 1