uva 694 - The Collatz Sequence

这道题值得一提的是,用int会超出运算范围,所以while里面会陷入死循环而超时。
故要用long long.
顺便地,int 范围差不多在 2,000,000,000 二十亿左右,看测试数据都知道超int了。

/*
这道题值得一提的是,用int会超出运算范围,所以while里面会陷入死循环而超时。
故要用long long.
顺便地,int 范围差不多在 2,000,000,000 二十亿左右,看测试数据都知道超int了。 
*/
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    long long a, b, cases = 1;
    
    while (cin >> a >> b, a >= 0 && b >= 0)
    {
        int ans = 1, A = a;
        
        while (a != 1)
        {
            if (a%2)
                a = a*3+1;
            else
                a = a/2;
            
            if (a > b)
                break;
                
            ans++;
        }
        
        printf("Case %d: A = %d, limit = %d, number of terms = %d\n", cases, A, b, ans);
        
        cases++;
    }
}

 

相关文章:

  • 2021-06-09
  • 2022-01-31
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
猜你喜欢
  • 2022-12-23
  • 2021-05-30
  • 2021-07-01
  • 2021-06-26
  • 2021-04-13
  • 2021-08-16
  • 2021-11-23
相关资源
相似解决方案