UVA - 694 - The Collatz Sequence

来源:互联网 发布:淘宝店铺的图片 编辑:程序博客网 时间:2024/05/19 13:26

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=635


题意:

  输入数字A,分段函数处理。水


#include <iostream>#include <stdio.h>using namespace std;int main(){long long num, A, L;long long iCase = 0;while ( 1 ){cin >>num >>L;A = num;if ( A < 0 && L < 0 ){break;} // end ifiCase++;long long nTerm = 0;while ( 1 ){if ( A > L ){break;} // end ifelse{nTerm++;if ( A == 1 ){break;} // end ifelse{if ( A%2 == 0 ){A = A/2;} // end ifelse{A = 3*A+1;} // end else} // end else} // end else} // end whilecout <<"Case " <<iCase <<": A = " <<num<<", limit = " <<L<<", number of terms = " << nTerm<<'\n';} // end whilereturn 0;}


0 0
原创粉丝点击