一个难题

问题描述:
GOJ给C_Shit_Hu,super13,TestN,LOP带来了一个难题:给了a和b,如何知道a ^ b。每个人都反对这个BT问题,所以GOJ使这个问题比开始容易。
这个难题描述:
给了a和b,如何知道a ^ b的最后一位数字。

输入:
有多个测试用例。每个测试用例由两个数字a和b(0 <a,b <= 2 ^ 30)组成

输出:
对于每个测试用例,您应该输出a ^ b的最后一位数字。

输入样例:
7 66
8 800
样本输出:
9
6

注意到检测数字较大,容易造成超时,无法使用常规方法运算

解决方案:

#include <stdio.h>
#include <string.h>

int main(void){
	int ans[8];
	int index;  //答案数组下标
	int cnt, n, tmp, i;
	while(scanf("%d %d", &n, &cnt) != EOF){
	
		//清空答案
		index = 0;		
		memset(ans, 0, sizeof(ans));    
		
	    n %= 10;
		tmp = n;
		
		if(!cnt){
			printf("%d\n" ,ans);
			continue;
		}

		//进行答案的输入
		while(1){
			i = 0;
			
			//查重
			while(i < index){
				if(ans[i++] != tmp){    //未重复
					continue;
				}
				else{   //检查到重复
					i = -1;
					break;
				}
			}

			//存答案
			if(i != -1){
				ans[index++] = tmp;
			}
			else{
				break;
			}

			tmp = (n*tmp)%10;
		}

		printf("%d\n", ans[(cnt-1)%index]);
		
	}

	return 0;
}
发布了7 篇原创文章 · 获赞 0 · 访问量 249

猜你喜欢

转载自blog.csdn.net/u012342808/article/details/103933818