UVa 100 3n+1
来源:互联网 发布:linux怎么更改root密码 编辑:程序博客网 时间:2024/06/06 18:09
这道题,wa死我了!宇神要是不提醒我,我估计我还wa着呢!
题不难想,但是陷阱是不少!
首先,输入的i和j不一定是从小到大;其次,i和j的输出位置不可以调换;还有,ans和cnt还有f[N]最好是用longlong类型;最后,要注意一个f的大小,用f存储的时候,要判断一下下标是否越界,最大的tmp不能超过N!
AC实属不易,考虑问题要全面,清楚!
优化的后的代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;long long f[1000001];int n, m;int main(){ memset( f, -1, sizeof(f)); while ( scanf("%d%d", &n, &m) != EOF ) { long long ans = 0; int s = min( n, m ), e = max(n, m); for ( int i = s; i <= e; ++i ) { if ( f[i] != -1 ) { ans = max( ans, f[i] ); continue; } int tmp = i; long long cnt = 1; while ( tmp != 1 ) { if ( tmp % 2 ) tmp = ( tmp*3 ) + 1; else tmp /= 2; if ( tmp < 1000000 && f[tmp] != -1 ) { cnt += f[tmp]; break; } cnt++; } f[i] = cnt; ans = max( cnt, ans ); } printf("%d %d %lld\n", n, m, ans); }}
- UVa 100 3n+1
- uva 100 3n+1
- UVA 100 The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- Uva 100 3n+1problem
- uva 100 The 3n+1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 The 3n + 1 problem
- uva 100 The 3n + 1 problem
- UVa 100 The 3n + 1 problem
- UVa 100 The 3n + 1 problem
- uva--100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa - 100 - The 3n + 1 problem
- JUnit3的使用
- /etc/shadow字段详解
- Android应用程序的自动更新升级(自身升级、通过tomcat)
- presentModalViewController 设置动画效果
- 判断处理器是大端还是小端
- UVa 100 3n+1
- Oracle Tuning Log File Sync 等待事件的几种策略
- AfxFrameOrView42s
- swprintf_s 实现字符串的拼接
- 学习策略
- Intel MIC适合什么样的应用程序
- busybox1.19.3编译错误解决办法
- Codeigniter 基本配置
- ObjC: 使用KVC