当前位置:编程学习 > C/C++ >>

(1.1.5)UVA 694 The Collatz Sequence (直叙式模拟)

 
/* 
 * UVA_694.cpp 
 * 
 *  Created on: 2013年10月6日 
 *      Author: Administrator 
 */  
  
#include <stdio.h>  
  
int main(){  
    long long a,l;//不要用int  
    int counter = 1;  
    while(scanf("%lld%lld",&a,&l)!=EOF,a!=-1 || l!= -1){  
  
        long long a1 = a;  
  
        int count = 1;  
  
        while(a != 1 ){  
  
            if( a%2 == 0 ){  
                a /= 2;  
            }else{  
                a = 3*a + 1;  
            }  
  
            if(a > l){  
                break;  
            }  
            count++;  
        }  
  
        printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n",counter++,a1,l,count);  
    }  
  
    return 0;  
}  

 


补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,