UVa 694 - The Collatz Sequence

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

直接下去模擬。

Code

UVa 694
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<cstdio>

int main()
{
int A, L;
int Case = 0;
while (scanf("%d%d", &A, &L) && A >= 0)
{
int count = 1;
long long n = A;
while (n != 1 )
{
if (n & 1)
n = 3 * n + 1;
else
n >>= 1;
if (n > (long long)L)
break;
count++;
}

printf("Case %d: A = %d, limit = %d, number of terms = %d\n", ++Case, A, L, count);
}

return 0;
}