UVALive 2037 Digital River

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

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 with 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


定义 river x :以x为首项,用x加上x的个位数字的和,得到下一项,依次类推得到的序列。

给出一个n,问 river n 和 river 1,3,9 最先在哪个位置有相同的取值


暴力求解

先做好river 1,3,9 对应的序列,用set 保存,然后求river n 的序列,判断所得的数是否在set里出现过。


/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<set>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int digit(int x)
{
	int ans=0;
	while(x)
	{
		ans+=x%10;
		x/=10;
	}
	return ans;
}
set<int> num[4];
int judge(int x)
{
	for(int i=0;i<=2;++i)
	{
		if(num[i].count(x))
		{
			return pow(3,i);//刚好结果是3的次幂
		}
	}
	return 0;
}
int main()
{
	num[0].insert(1);
	num[1].insert(3);
	num[2].insert(9);
	for(int i=0;i<=2;++i)
	{
		int tp=pow(3,i);
		while(tp<maxn)
		{
			tp+=digit(tp);
			num[i].insert(tp); 
		}
	}
	int kase=0,n;
	while(scanf("%d",&n),n)
	{
		num[3].clear();
		if(kase++)
		{
			printf("\n");
		}
		num[3].insert(n);
		int tp=n,ok;
		while(tp<maxn)
		{
			ok=judge(tp);
			if(ok!=0)
			{
				break;
			}
			tp+=digit(tp);
		}
		printf("Case #%d\nfirst meets river %d at %d\n",kase,ok,tp);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值