zoukankan      html  css  js  c++  java
  • UVALive 2037 Digital Rivers 【打表&二分】

     Digital Rivers
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    Download as PDF
     

    A digital river is a sequence of numbers where the number following n is n plus the sum of its digits. For example, 12345 is followed by 12360, since 1 + 2 + 3 + 4 + 5 = 15. If the first number of a digital river is k we will call it river k.

    For example, river 480 is the sequence beginning {480, 492, 507, 519,...} and river 483 is the sequence beginning {483, 498, 519,...}.

    Normal streams and rivers can meet, and the same is true for digital rivers. This happens when two digital rivers share some of the same values. For example: river 480 meets river 483 at 519, meets river 507 at 507, and never meets river 481.

    Every digital river will eventually meet river 1, river 3 or river 9. Write a program that can determine for a given integer n the value where river n first meets one of these three rivers.

     

    Input

    The input may contain multiple test cases. Each test case occupies a separate line and contains an integer n(1$ le$n$ le$16384). A test case wit h value of 0 for n terminates the input and this test case must not be processed.

     

    Output

    For each test case in the input first output the test case number (starting from 1) as shown in the sample output. Then on a separate line output the line ``first meets river x at y". Here y is the lowest value where river n first meets river x (x = 1 or 3 or 9). If river nmeets river x at y for more than one value of x, output the lowest value.

    Print a blank line between two consecutive test cases.

     

    Sample Input

     

    86 
    12345 
    0
    

     

    Sample Output

     

    Case #1 
    first meets river 1 at 101 
    
    Case #2
    first meets river 3 at 12423
    <pre name="code" class="cpp">#include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <iostream>
    #define MAX_N   15000
    #define MAX(a, b) (a > b)? a: b
    #define MIN(a, b) (a < b)? a: b
    using namespace std;
    
    int r9[MAX_N], r1[MAX_N], r3[MAX_N];
    //进行数字流的递推
    int River(int x) {
        int k = x;
        int ans = x;
        while (k) {
            ans += k%10;
            k /= 10;
        }
        return ans;
    }
    //进行打表
    void init() {
        r1[0] = 1, r3[0] = 3, r9[0] = 9;
        for (int i = 1; i < 2005; i++) {
            r1[i] = River(r1[i - 1]);
            r3[i] = River(r3[i - 1]);
            r9[i] = River(r9[i - 1]);
        }
    }
    //二分查找,节约时间
    int lowerbound(int x, int a[]) {
        int lb = -1, ub = 2000;
        while (ub - lb > 1) {
            int mid = (lb + ub)/2;
            if (a[mid] > x) {
                ub = mid;
            } else {
                lb = mid;
            }
        }
        return ub;
    }
    int main() {
        int n;
        init();
        int m = 5;
        bool flag = false;
        int cnt = 0;
        while (scanf("%d", &n), n) {
            if (flag)   printf("
    ");
            if (!flag )  flag = true;
            printf("Case #%d
    ", ++cnt);
            //进行查找
            while (true) {
                int a = lowerbound(n, r1);
                int b = lowerbound(n, r3);
                int c = lowerbound(n, r9);
                a--, b--, c--;
                if (n == r1[a]) {
                    printf("first meets river 1 at %d
    ", n);
                    break;
                }
                else if(n == r3[b]) {
                    printf("first meets river 3 at %d
    ", n);
                    break;
                }
                else if (n == r9[c]) {
                    printf("first meets river 9 at %d
    ", n);
                    break;
                }
                n = River(n);
            }
        }
        return 0;
    }


    
    
  • 相关阅读:
    SpringBoot中mybatis配置自动转换驼峰标识没有生效
    spring boot 配置动态刷新
    读书笔记——spring cloud 中 HystrixCommand的四种执行方式简述
    spring cloud 加入配置中心后的 部分 配置文件优先级
    spring boot 服务 正确关闭方式
    CentOS 6.4 安装 rabbitmq(3.6.15)
    CentOS 6.4 配置DNS
    CentOS 查看系统版本号
    服务治理的技术点
    【转载】C#中使用Average方法对List集合中相应元素求平均值
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770936.html
Copyright © 2011-2022 走看看