【題解】1068. Weird Algorithm

【題解】1068. Weird Algorithm

鯊魚Y.Y Lv2

1068. Weird Algorithm

想法

根據題目判斷奇偶然後做 3*n+1 或 n/2 做到 n 剩下1

AC Code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin>>n;
for(;;){
cout<<n;
if(n==1)break;
cout<<' ';
n=(n%2 ? 3*n+1 : n/2);
}
cout<<'\n';
}

題解

我用了一個斜咖無限迴圈用法,但跟while其實一樣,然後n用三元運算值去判斷奇偶數做更新

  • 標題: 【題解】1068. Weird Algorithm
  • 作者: 鯊魚Y.Y
  • 撰寫於 : 2026-11-11 13:34:19
  • 更新於 : 2026-11-11 13:48:45
  • 連結: https://sharkyy.org/post/CSES-1068/
  • 版權宣告: 本作品採用 CC BY-NC-SA 4.0 進行許可。
留言
目錄
【題解】1068. Weird Algorithm