uvaoj 694 – The Collatz Sequence

uvaoj 694 – The Collatz Sequence

/*
刘哥啊
你可把我害惨了啊
你口口声声提醒要用long long 型
我就傻傻的用long long 啊
有木有! 
C语言%lld要加stdlib
读-1还老出错啊
有木有! 
c语言过不了再用C++
才发现原来cin不支持long long 型啊!!!
有木有! 
后来实在纠结就百度了一下
原来人家用的是double啊
有木有! 
什么浮点误差啊
都被人家当成了浮云啊,有木有!!! 
有木有!!!!!!!!! 
*/
#define LOCAL    
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
#ifdef LOCAL
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
#endif
    
    double n,limit,a;
    long c=1,term;
    while(1)
    {
         cin>>n>>limit;
         a=n;
         if(n==-1&&limit==-1) return 0;
         term=1;
         while(n!=1&&n<=limit)
         {
                if(fmod(n,2)==0) n=n/2;
                else n=3*n+1;
                if(n>limit)
                break;
                term++;                       
         }
         cout<<"Case "<<setiosflags(ios::fixed) << setprecision(0)<<    //别忘了设置精度,不然会出错滴 
         c++<<": A = "<<setiosflags(ios::fixed) << setprecision(0)<<
         a<<", limit = "<<setiosflags(ios::fixed) << setprecision(0)<<
         limit<<", number of terms = "<<setiosflags(ios::fixed) << setprecision(0)<<
         term<<endl;
    }
    return 0;
}

发表回复