ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Collatz Sequence

Follow the halve-or-triple-and-add-one rule until reaching 1.

Start with n. If it is even, divide it by 2; if it is odd, multiply it by 3 and add 1. Repeat until the value is 1. Include both the starting value and the final 1 in your answer.

Implement collatz_sequence(n).

Input

n: The positive starting integer.

Output

Return the complete sequence in order.

เงื่อนไข

1 ≤ n ≤ 10,000. Inputs in this range reach 1 under this rule; all sequence values fit a signed 64-bit integer.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 6

ผลลัพธ์ที่คาดหวัง:
expected = [6, 3, 10, 5, 16, 8, 4, 2, 1]
อินพุต:
long long n = 6;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {6, 3, 10, 5, 16, 8, 4, 2, 1};
อินพุต:
long n = 6;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {6, 3, 10, 5, 16, 8, 4, 2, 1};

คำอธิบาย

6 is even, so it becomes 3. Then 3 becomes 10. Continue applying the rule until 1.

ตัวอย่างที่ 2
อินพุต:
n = 1

ผลลัพธ์ที่คาดหวัง:
expected = [1]
อินพุต:
long long n = 1;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {1};
อินพุต:
long n = 1;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {1};

คำอธิบาย

The starting value is already 1, so no transformations are needed.

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก