UVALive 2037 Digital Rivers 【打表&二分】
2016-07-19 14:24
465 查看
Digital Rivers
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
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
n
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
Sample Output
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
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
n
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 #2first 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("\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; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata