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;
    }


    
    
  • 相关阅读:
    oracle判断是否包含字符串的方法
    linux环境下卸载mysql
    分辨率与px的关系
    plsql登录,tables表为空解决方案
    excle导出、导入、下载_jeesite注解@ExcelField
    安卓中常用的弹出框
    c#将数据导出到excel中
    将表里的数据分组,并取每组中的最大的一条数据
    c#如何写服务,打包和卸载服务
    C#排列组合类,写彩票算法的朋友们可以来看一看
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770936.html
Copyright © 2011-2022 走看看