求助各位大佬,这道题为什么不可以用do-while循环?
查看原帖
求助各位大佬,这道题为什么不可以用do-while循环?
1084226
fz6668楼主2023/9/24 21:23
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
	int a[1000],temp,n=0;
	cin>>temp; 
	do{
		a[n]=temp;
		if(temp%2==0){
			temp/=2;
		}else{
		
			temp=temp*3+1;
		}
		n++;
	}while(temp!=1);
	a[n]=1;
	for(int j=n;j>=0;j--){
		cout<<a[j]<<' ';
	}
	return 0;
} 

输入1的时候,输出结果不应该是1 2 4 1吗?

2023/9/24 21:23
加载中...