博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive 2037 Digital Rivers 【打表&二分】
阅读量:6580 次
发布时间:2019-06-24

本文共 3019 字,大约阅读时间需要 10 分钟。

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

Description

 

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
#include 
#include
#include
#include
#include
#include
#include
#define MAX_N 15000#define MAX(a, b) (a > b)? a: b#define MIN(a, b) (a < b)? a: busing 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("\n"); if (!flag ) flag = true; printf("Case #%d\n", ++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", n); break; } else if(n == r3[b]) { printf("first meets river 3 at %d\n", n); break; } else if (n == r9[c]) { printf("first meets river 9 at %d\n", n); break; } n = River(n); } } return 0;}
 

转载于:https://www.cnblogs.com/cniwoq/p/6770936.html

你可能感兴趣的文章
CF294C Shaass and Lights
查看>>
oracle 11g 报错记录
查看>>
文件状态是否变化
查看>>
MongoDB的副本集Replica Set
查看>>
Maven项目中的配置文件找不到以及打包问题
查看>>
面向对象
查看>>
HDU 1058 Humble Numbers
查看>>
NYOJ The Triangle
查看>>
wps10.1中将txt转为excel
查看>>
并发同步知多少
查看>>
解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题...
查看>>
[BZOJ3312][USACO]不找零(状压DP)
查看>>
页面之间的传值和大量参数的传递
查看>>
python学习之路-5 基础进阶篇
查看>>
gtp转换mbr
查看>>
django rest framework
查看>>
登录注册界面
查看>>
poj1985 求树的直径
查看>>
Python PyPI中国镜像
查看>>
centos 设置静态IP
查看>>