因为你每次输入都要计算一次,所以容易超时。建议用个二维数组将所有的情况全部储存起来。然后直接输出,我的你可以借鉴一下,在杭电上ac过了
#include
void main()
{
int a[10][30],i,j,k;
for(i=0;i<10;i++)
{
a[i][1]=i;
k=i;
for(j=2;j<10;j++)
{
a[i][j]=(k*a[i][j-1])%10;
if(a[i][j]==a[i][1])
{
a[i][0]=j-1;
break;
}
}
}
while(scanf("%d%d",&i,&j)!=EOF)
{
if(j!=0)
{
i=i%10;
j=(j-1)%a[i][0]+1;
printf("%d\n",a[i][j]);
}
else
printf("1\n");
}
}
b太大了,,这样做超时是肯定的,,其实里面有规律,找出规律直接打表就行了
C代码,很直白的说~
==============================================================
#include
int main()
{
long a,b,i,t,k;
int s[10][4]={
{0,0,0,0},
{1,1,1,1},
{6,2,4,8},
{1,3,9,7},
{6,4,6,4},
{5,5,5,5},
{6,6,6,6},
{1,7,9,3},
{6,8,4,2},
{1,9,1,9}};
while(scanf("%ld%ld",&a,&b)!=EOF)
{
t=a%10;
k=b%4;
printf("%ld\n",s[t][k]);
}
return 0;
}
==============================================================