“3n+1”改编 关键是控制输出格式
AC代码:
#include<iostream>
using namespace std;
int main()
{
int k;
cin >> k;
while (k--)
{
int a;
cin >> a;
int flag = 1; //无输出的标志
int k = 0;//控制空格
while (a != 1)
{
if (a % 2)
{
if (k++)cout << " ";
cout << a;
a = 3 * a + 1;
flag = 0;
}
else a /= 2;
}
if (flag)cout << "No number can be output !" << endl;
else cout << endl;
}
// system("pause");
return 0;
}