高质量题解|A7825.角谷猜想
2026-01-23 16:44:18
发布于:北京
3阅读
0回复
0点赞
解题思路
根据题目描述,我们得知如果这个数为奇数,那么乘3再加1;如果是偶数,就除以2
最后能得到1,而且不到1就继续刚才的操作,这说明我们需要用while来解决
如果最后是1了,就结束,并输出“End”
大致就是这样,具体看代码
代码
#include <bits/stdc++.h>
#include <ostream>
using namespace std;
int main(){
int N;
cin >> N;
if(N == 1){
cout << "End";
}
while(N != 1){
if(N % 2 != 0){
cout << N;
N = 3 * N + 1;
cout << "*3+1=" << N << endl;
}
if(N % 2 == 0){
cout << N;
N /= 2;
cout << "/2=" << N << endl;
}
if(N == 1){
break;
}
}
cout << "End";
return 0;
}
这里空空如也







有帮助,赞一个