不理解为什么超时了,复试上机题

问题如下:
描述
对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。 请计算需要经过几步才能将n变到1,具体可见样例。
输入描述:
测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)
输出描述:
对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。

运行结果及报错内容

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int n;
    cin>>n;
    while(n!=0){
        int m=0;
        while(n!=1){
            if(n%2==0)
                n=n/2;
            else
                n=(n*3+1)/2;
            m++;
        }
        cout<<m<<endl;
        cin>>n;
    }
    return 0;
}

出现了超时错误。

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int n;
    while(cin>>n&&n!=0){
        int m=0;
        while(n!=1){
            if(n%2==0)
                n=n/2;
            else
                n=(n*3+1)/2;
            m++;
        }
        cout<<m<<endl;
    }
    return 0;
}

未超时
为什么前面一个会超时呀?


#include <iostream>
#include <cstdio>
using namespace std;
int main() {
    int n;
    if (!(cin >> n)) {
        cout << "输入错误" << endl;
        n = 0;
    }
    while (n != 0) {
        int m = 0;
        while (n != 1) {
            if (n % 2 == 0)
                n = n / 2;
            else
                n = (n * 3 + 1) / 2;
            m++;
        }
        cout << m << endl;
        if (!(cin >> n)) {
            cout << "输入错误" << endl;
            n = 0;
            continue;
        }
    }
    return 0;
}