694 - The Collatz Sequence
2012-04-18 13:26
211 查看
Step 1: Choose an arbitrary positive integer A as the first item in the sequence.
Step 2: If A = 1 then stop. Step 3: If A is even, then replace A by A / 2 and go to step 2.
Step 4: If A is odd, then replace A by 3 * A + 1 and go to step 2.
简单题。唯一值得一提的是int有可能越界导致超时(死循环),用long来保存。
Step 2: If A = 1 then stop. Step 3: If A is even, then replace A by A / 2 and go to step 2.
Step 4: If A is odd, then replace A by 3 * A + 1 and go to step 2.
简单题。唯一值得一提的是int有可能越界导致超时(死循环),用long来保存。
#include<stdio.h> int main() { int count=1,sum; long long A,L,temp; while(scanf("%lld%lld",&A,&L)) { if(A<0&&L<0)break; sum=1; temp=A; while(A!=1) { if(A%2!=0) { A=(A*3+1)/2; if(A*2>L)break; sum+=2; } else { A/=2; sum++; } } printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n",count++,temp,L,sum); } return 0; }
相关文章推荐
- uva694 The Collatz Sequence
- uva694 The Collatz Sequence
- 694 - The Collatz Sequence
- UVA694 UVALive5466 The Collatz Sequence【模拟】
- UVA 694 - The Collatz Sequence
- 【UVA】【第0章】694 - The Collatz Sequence
- UVa_694 - The Collatz Sequence
- uvaoj 694 - The Collatz Sequence
- 694 - The Collatz Sequence
- UVa 694 The Collatz Sequence
- UVa 694 - The Collatz Sequence
- 694 - The Collatz Sequence---关于goto语句的一些反思(还有uva的Runtime Error问题的解决)
- UVA 694-The Collatz Sequence
- 694 - The Collatz Sequence
- UVaOJ 694 - The Collatz Sequence
- UVA 694 The Collatz Sequence
- UVa694 - The Collatz Sequence-难度1
- UVaOJ 694 - The Collatz Sequence
- UVA - 694 The Collatz Sequence(水题)
- UVA694 - The Collatz Sequence