杭电1097题目:下面是我的编程,请各位指教,为什么老师说time limited

Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output
For each test case, you should output the a^b's last digit number.

Sample Input
7 66
8 800

Sample Output
9
6

#include<stdio.h>
int main()
{
int a,b,c;
while(scanf("%d%d",&a,&b)!=-1)
if(a>0&&b>0){
c=1;
while(b--){
c*=a;
c=c%10;
}
printf("%d\n",c);}
return 0;
}

请先 登录 后评论

1 个回答

好问星星

//
#include
int main()
{
int i, n, a, b;
while(scanf("%d%d", &a, &b) != EOF)
{
n = a % 10;
switch(n)
{
case 0:
case 1:
case 5:
case 6:
printf("%d", n); //尾数为0,1,5,6 时, a^b以后还是一样的
break;
case 2: //尾数为2
if(b % 4 == 0)printf("6");
else if(b % 4 == 1)printf("2");
else if(b % 4 == 2)printf("4");
else printf("8");
break;
case 3: //尾数为3
if(b % 4 == 0)printf("1");
else if(b % 4 == 1)printf("3");
else if(b % 4 == 2)printf("9");
else printf("7");
break;
case 4:
if(b % 2)printf("4");
else printf("6");
break;
case 7:
if(b % 4 == 0)printf("1");
else if(b % 4 == 1)printf("7");
else if(b % 4 == 2)printf("9");
else printf("3");
break;
case 8:
if(b % 4 == 0)printf("6");
else if(b % 4 == 1)printf("8");
else if(b % 4 == 2)printf("4");
else printf("2");
break;
case 9:
if(b % 2)printf("9");
else printf("1");
break;
}
printf("n");
}
}
//祝学习进步,更上一层楼 *^_^* !

请先 登录 后评论
  • 1 关注
  • 0 收藏,64 浏览
  • 提出于 2022-09-12 08:58

相似问题