UVA - 694 The Collatz Sequence
来源:互联网 发布:淘宝好吃的点心店铺 编辑:程序博客网 时间:2024/05/20 11:35
题目大意:给出数字 A 和范围 L,在范围 L 内运算,当 A 为偶数 A = A/2,当 A 为奇数 A = 3×A+1,当 A = 1 时停止。问运算次数。
解题思路:简单模拟。运算过程中可能会超出 int 范围所以用 long long,溢出提示是 TLE 还以为算法有问题,结果想多了,一把心酸泪。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int cnt;int tot = 0;int main() { int A, L; while (scanf("%d%d", &A, &L)!= EOF && A != -1 && L != -1) { long long n = A; cnt = 1; while (n != 1 && n <= L) { if (n%2) n = 3*n+1; else if (!(n%2)) n /= 2; cnt++; } if (n != 1) cnt--; printf("Case %d: A = %d, limit = %d, number of terms = %d\n", ++tot, A, L, cnt); }return 0;}
0 0
- UVa 694 - The Collatz Sequence
- uva-694 - The Collatz Sequence
- UVa 694 The Collatz Sequence
- UVA 694 - The Collatz Sequence
- UVA 694 - The Collatz Sequence
- UVa 694 - The Collatz Sequence
- UVA - 694 - The Collatz Sequence
- UVa 694 - The Collatz Sequence
- UVa 694 The Collatz Sequence
- UVA 694 The Collatz Sequence
- UVA 694 The Collatz Sequence
- UVA 694 The Collatz Sequence
- UVA 694-The Collatz Sequence
- UVA - 694 The Collatz Sequence
- UVA - 694 The Collatz Sequence
- uva The Collatz Sequence
- UVa 694 The Collatz Sequence(序列)
- UVa 694 The Collatz Sequence (数论)
- Centos 7 中文显示乱码解决办法
- javascript中的回调函数【理解记忆】
- 欢迎使用CSDN-markdown编辑器
- 【SSH】Struts2.3+Spring4.3+Hibernate3整合
- php函数
- UVA - 694 The Collatz Sequence
- 管理还原数据-管理还原数据
- android ble
- 正则表达式
- 100天土鸡饲养计划(25)
- 文章标题
- hdu 5835 Danganronpa(贪心,优先队列)
- Android自定义流式布局/自动换行布局
- STM8 ADC