【UVALive】2037 - Digital Rivers(找规律,暴力)
来源:互联网 发布:美工图片 编辑:程序博客网 时间:2024/06/06 16:28
点击打开题目
2037 - Digital Rivers
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 isk 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(1n16384). 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 n meets 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
首先要找规律,题目给出了,x只能是 1、3、9,那么先看一下1、3、9这几个开始的包括的数有哪些。
1后面的数比较杂,没有什么规律,但是有一个关键的地方:没有3或9的倍数。
3后面的数都是3的倍数,但不是9的倍数。
9后面的数都是9的倍数。
那么就很好找了,把给出的 n 每一位都加起来,先判定是否是9的倍数,是的话 x = 9 ;否则判断是否是3的倍数,是的话 x = 3;否则 x = 1;
然后就往后找数就行了。
代码如下:
#include <cstdio>int dis(int x)//计算每一位的和{int sum = 0;while (x){sum += x % 10;x /= 10;}return sum;}int cal(int a,int b){while (a != b){if (a > b)b += dis(b);elsea += dis(a);}return a;}int main(){int n;int x;int ans;int num = 1;while (~scanf ("%d",&n) && n){int sum = dis(n);if (num > 1)printf ("\n");printf ("Case #%d\n",num++);if (sum % 9 == 0){printf ("first meets river 9 at ");ans = cal(n , 9);}else if (sum % 3 == 0){printf ("first meets river 3 at ");ans = cal(n , 3);}else{printf ("first meets river 1 at ");ans = cal(n , 1);}printf ("%d\n",ans);}return 0;}
- 【UVALive】2037 - Digital Rivers(找规律,暴力)
- UVALive 2037 Digital Rivers 【打表&二分】
- hdu 1013 Digital Roots (找规律)
- UVALive 6847 Zeroes(找规律)
- UVALive 7045 Last Defence(找规律)
- 【找规律】UVALive
- hdu 4708(暴力+找规律)
- hdu 5084 HeHe(暴力/找规律)
- hdu5917Instability(找规律+暴力)
- UVALive 6270 Edge Case(找规律,大数相加)
- UVAlive-6577 Binary Tree(递推+找规律)
- UVALive 2037 Digital River
- SGU 118. Digital Root 找规律+模拟
- 找规律 hdu1163 Eddy's digital Roots
- UVA 10970 Big Chocolate(暴力,找规律)
- UVALive 4485 Crossing Rivers
- UVALive - 6832 Bit String Reordering (找规律)
- Beehive UVALive - 7528 (找规律+数学思维)
- C++類庫文件使用(不用.h文件)
- Android开发,ListView的OnItemClickListener被里面的Button的OnClickListener拦截
- C++ 语法:C++变量声明和定义的区别
- 最小生成树和prim算法
- linux内核input子系统解析
- 【UVALive】2037 - Digital Rivers(找规律,暴力)
- Windows下 tuxedo 安装及测试
- 动态规划(4)交替字符串
- C++ vector 实现
- hdu 5175 Misaki's Kiss again(数论)
- 375. Guess Number Higher or Lower II
- scrapy抓取动态页面方法
- windows中运行qt5构建的程序提示 无法启动此程序,因为计算机中丢失qt5Cored.dll 解决方法
- MySQL 模糊查询