问栈的实现方法
查看原帖
问栈的实现方法
1041765
AKkkk_TIX楼主2024/8/9 13:12
#include <bits/stdc++.h>
using namespace std;
stack<int> st;
int n, i;
int main(){
    cin >> n;
    st.push(n);
    for(i = 0;n != 1;i++){
        if(n % 2 == 0){
            n /= 2;
            st.push(n);
        }
        else{
            n *= 3;
            n++;
            st.push(n);
        }
    }
    st.push(1);
    for(int j = 0;j < i;i++){
        cout << st.top() << " ";
        st.pop();
    }
    
    return 0;
}

记录详情

2024/8/9 13:12
加载中...