2018 青岛区域赛 M - Function and Function

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kongsanjin/article/details/83990100

                                     Function and Function

Sample Input

6
123456789 1
888888888 1
888888888 2
888888888 999999999
98640 12345
1000000000 0

Sample Output

5
18
2
0
0
1000000000

题意描述:

根据表格中每个数字对应的值,求出经过K次关系转化,输出最后的结果。如f(123)=0+0+0=0;就是数字1、2、3每个数字对应的数值再求和。而f^2(123)就是f(f(123))=f(0)=1;

解题思路:

签到题,当经过转化次数多后,最后结果不是0就是1;利用函数进行转化,当转化为0时不在转化,判断还需要转化的次数,如果剩余奇数次结果就为1,偶数次为0;

(判断奇偶,可用按位与,“&”,(k&1)为真k即为奇数)

程序代码:

#include <stdio.h>
int mm[] = {1,0,0,0,1,0,1,0,2,1};
int mmm(int n)
{
	int ans = 0;
	if (n==0)
		return 1;
	while(n)
	{
		ans+=mm[n%10];
		n/=10;
	}
	return ans;
}
int main()
{
	int t, x, k, n;
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d%d", &x, &k);
		while(k)
		{
			x=mmm(x);
			k--;
			if(x==0)
				break;
		}
		if(k)
		{
			if(k & 1)
				x=1;
		}
		printf("%d\n", x);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/kongsanjin/article/details/83990100