#include<iostream>
using namespace std;
int x, y, x1;
int main() {
cout << "enter a number :";
y = 0;
cin >> x;
x1 = x % 2;
while (x != 1) {
if (x1 == 0) { x = x / 2; }
else { x = (x * 3 + 1) / 2; }
y = y + 1;
}
cout << endl << y;
return 0;
}
你这个代码写的有逻辑问题,会进入死循环。你的题目是什么
因为cout遇到endl就结束了
你应该把y夹中间
#include<iostream>
using namespace std;
int x, y, x1;
int main() {
cout << "enter a number :";
y = 0;
cin >> x;
x1 = x % 2;
while (x != 1) {
if (x1 == 0) { x = x / 2; }
else { x = (x * 3 + 1) / 2; }
y = y + 1;
}
cout << endl << y;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int x, y, x1;
int main()
{
cout << "enter a number :";
y = 0;
cin >> x;
while (x != 1)
{
x1 = x % 2;
if (x1 == 0)
{
x = x / 2;
}
else
{
x = (x * 3 + 1) / 2;
}
y = y + 1;
}
cout << endl << y;
return 0;
}