UVa 3n+1 问题
2011-08-29 16:26
309 查看
《挑战编程》第一个问题3n+1 看似非常简单的问题。但如果只是简单的使用蛮力法,就是超时的结果。所以在计算一个数n,它的节数的同时。需要把它经过的数记录下来。因为这些数的节数都与n有关。在查找n的同时,可以把这些数的节数同时也找出来。 代码如下:
这个题目提醒我们,应该考虑较多的是时间复杂度。毕竟现在的计算机的内存都是非常大的。我们编写的程序如果只是多占用几MB或十几MB,却能大大的降低时间复杂度,这又何乐不为。
#include <vector> #include <stdio.h> #include <iostream> using namespace std; const int scale = 5000000; int answer[scale + 1] = {0}; vector< unsigned int > Numvec; // 查找num的值及其经过的数的值 void funfind( unsigned int num); // 处理栈中的经过的数 void process(); /* * 递归查找经过的各个数的 节数。 * 对于同一个数将不会重复查找 * */ void funfind(unsigned int num) { Numvec.push_back( num ); if ( !(num % 2) && num / 2 > scale ) // 为偶数, 且超值了。 { funfind( num / 2); } else if( !(num % 2) && num / 2 <= scale && answer[ num / 2 ] ) { Numvec.push_back( num / 2 ); process(); } else if( !(num % 2) && num / 2 <= scale && answer[ num / 2 ] == 0 ) { funfind( num / 2); } else { funfind( 3 * num + 1); } } /* * 处理队列中的各个数,计算其节数。 * */ void process( ) { size_t vecsize = Numvec.size(); int addNumber = answer[ Numvec[ vecsize -1 ] ]; vector< unsigned int >::iterator end = Numvec.end(); for(vector<unsigned int >::iterator it = Numvec.begin(); it != end; ++ it) { if( (*it) < scale ) answer[ *it ] = vecsize - 1 + addNumber; vecsize --; } Numvec.clear(); } int main( int argc, char ** argv) { int low , high; int max = 0; int pos = 0 ; answer[1] = 1; answer[2] = 2; while( scanf("%d %d",&low, &high ) != EOF ) { max = 0; cout << low << " " << high << " "; if( low > high ) { int temp = low; low = high; high = temp; } for(int i = low; i <= high; ++i ) { if(!( answer[i] ) ) funfind(i); if ( max < answer[i] ) { max = answer[i]; pos = i; } } cout << max << endl; } return 0; }
这个题目提醒我们,应该考虑较多的是时间复杂度。毕竟现在的计算机的内存都是非常大的。我们编写的程序如果只是多占用几MB或十几MB,却能大大的降低时间复杂度,这又何乐不为。
相关文章推荐
- UVa Problem 100 The 3n+1 problem (3n+1 问题)
- UVA-694-限制性的3n+1问题
- UVA 100 - The 3n+1 problem (3n+1 问题)
- UVA 100 - The 3n+1 problem (3n+1 问题)
- UVA 100 && HDU 1032 The 3n+1问题 (扑街题)
- 1.6.1 3n+1问题 UVA100
- 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)
- UVa Problem 100 The 3n+1 problem (3n+1 问题)
- PC/UVa 110101/100 3n+1 问题(3n+1 Problem)
- UVa Problem 100 The 3n+1 problem (3n+1 问题)+1 问
- PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)
- uva 571 - Jugs(倒水问题)
- UVa 10491 Cows and Cars (概率&广义三门问题 )
- 紫书章五例二 木块问题 UVA 101(vector)
- UVa839_(二叉树递归_天平问题)
- UVA165连续邮资问题
- 3n+1问题
- 【uva1380 - 一个调度问题】思路题+树形dp
- UVA - 571 - Jugs (数论 - 经典倒水问题)
- UVA 11538 - Chess Queen(数论+计数问题)