Rightmost Digit

时间: 1ms        内存:128M

描述:

Given a positive integer N, you should output the most right digit of N^N.

输入:

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

输出:

For each test case, you should output the rightmost digit of N^N.

示例输入:

2
3
4

示例输出:

7
6

提示:

参考答案(内存最优[1120]):

#include<stdio.h>
typedef long long ll;
ll mod=1e5;
ll pow(ll a,ll b){
    ll ans=1;while(b!=0){
        if(b%2==1)
            ans=ans*a%mod;
            a=a*a%mod;
            b=b/2;
    }
    return ans;
}
int main(){
	 #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ll n,m,ans;
    while(~scanf("%lld",&n)){
        while(n--){
            scanf("%lld",&m);
            ans=pow(m,m)%10;
            printf("%lld\n",ans);
        }
    }
    return 0;
}

参考答案(时间最优[1]):

#include <stdio.h>
int main()
{
    long int a,k,l;
    int n,c=0;
    scanf("%d",&n);
    while(scanf("%d",&a)!=EOF)
    {
        k=a%10;
        if(k==0)
        {printf("0\n");}
        if(k==1)
        {printf("1\n");}
        if(k==2||k==8)
        {l=a%4;
        if(l==0)
            printf("6\n");
        if(l!=0)
            printf("4\n");
        }
        if(k==3)
        {l=a%4;
          if(l==0)
            printf("1\n");
        if(l==1)
            printf("3\n");
        if(l==2)
            printf("9\n");
        if(l==3)
            printf("7\n");
        }
        if(k==4)
        {printf("6\n");}
        if(k==5)
            {printf("5\n");}
        if(k==6)
            {printf("6\n");}
        if(k==7)
        {l=a%4;
            if(l==0)
                printf("1\n");
            if(l==1)
                printf("7\n");
            if(l==2)
                printf("9\n");
            if(l==3)
                printf("3\n");
        }
        if(k==9)
        {printf("9\n");}
        c=c+1;
        if(c==n)
            break;
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。