zoukankan      html  css  js  c++  java
  • UVALive 7045 Last Defence(2014西安赛区现场赛)

    Given two integers A and B. Sequence S is defined as follow:
    • S0 = A
    • S1 = B
    • Si = |Si−1 − Si−2| for i ≥ 2
    Count the number of distinct numbers in S.

    Input:
    The first line of the input gives the number of test cases, T. T test cases follow. T is about 100000.
    Each test case consists of one line — two space-separated integers A, B. (0 ≤ A, B ≤ 1018).

    Output:
    For each test case, output one line containing ‘Case #x: y’, where x is the test case number (starting
    from 1) and y is the number of distinct numbers in S.

    Sample Input:
    2
    7 4
    3 5
    Sample Output:
    Case #1: 6
    Case #2: 5

    题意:有一个序列,它的规律是Sn = |Sn-1 - Sn-2|(n >= 2),现在知道S1和S2,求出这个序列中出现不同数字的个数。

    (我也不太明白为啥这样写,目测是根据观察得出的结论)

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int main ()
    {
        int T, k = 0;
        long long a, b, ans;
    
        scanf("%d", &T);
    
        while (T--)
        {
            scanf("%lld %lld", &a, &b);
    
            k++;
            ans = 0;
    
            if (a == 0 && b == 0) ///这里是最容易遗漏的地方
            {
                printf("Case #%d: %lld
    ", k, ans+1);
                continue;
            }
    
            if (a == 0 || b == 0) ans = 1;
    
            while (a || b)
            {
                if (a < b) swap(a, b);
    
                if (b == 0)
                {
                    ans++;
                    break;
                }
    
                ans += a / b;
                a = a % b;
            }
    
            printf("Case #%d: %lld
    ", k, ans);
        }
    
        return 0;
    }
  • 相关阅读:
    有用的网站
    RMVANNUAL matlab remove annual cycle of a time series
    [转载]grdcontour命令在GMT4下绘制等值线图
    Filter应用之-自动登录
    Filter应用之-验证用户是否已经登录
    Filter应用之2-设置某些页面缓存或是不缓存
    过虑器应用之1-设置request编码
    过滤器Filter
    java文件下载
    用COS实现文件上传
  • 原文地址:https://www.cnblogs.com/syhandll/p/4863959.html
Copyright © 2011-2022 走看看