Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu 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.
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 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;}