Fjy20130703 @ 2024-09-25 21:07:28
#include<bits/stdc++.h>
using namespace std;
int a;
int main(){
cin>>a;
while(a!=1){
if(a%2==1){
a*=3;
a+=1;
cout<<(a-1)/3<<"*3+1="<<a;
}else{
a/=2;
cout<<a*2<<"/2="<<a;
}
}
return 0;
}
by fxwqctb @ 2024-09-25 21:10:04
输出没换行
每加 End
by LosTcrab @ 2024-09-25 21:31:03
在每次输出后加上endl,换下行。特判一下a==1时输出End,t的话建议开下longlong和ios
by luoyebushiye @ 2024-09-25 21:33:03
@Fjy20130703
应该不是用
by tuonisitake @ 2024-09-25 23:52:36
我这样写你应该能看懂把。
#include <stdio.h>
int main()
{
long long n;
scanf("%lld",&n);
while (n!=1)
{
if(n%2==0)
{
printf("%lld/2=",n);
n/=2;
printf("%lld\n",n);
}
else if(n%2!=0){
printf("%lld*3+1=",n);
n=n*3+1;
printf("%lld\n",n);
}
}
printf("End");
return 0;
}
by Fjy20130703 @ 2024-09-26 19:14:40
Thank you
by Fjy20130703 @ 2024-09-26 19:18:37
以全部回关
by gaohuashengtx @ 2024-12-22 20:59:22
@Fjy20130703 这样写更简单
#include<bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin>>n;
while(n>1){
if(n%2==0){
cout<<n<<"/2="<<n/2<<endl;
if(n/2==1)break;
n/=2;
}
else{
cout<<n<<"*3+1="<<n*3+1<<endl;
n=n*3+1;
}
}
cout<<"End"<<endl;
return 0;
}
你可以试试