# Rightmost Digit

xiaoxiao2021-02-28  9

Problem Description

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

Input

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

Output

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

Sample Input

2

3

4

Sample Output

7

6

Hint

In the first case, 3 * 3* 3 = 27, so the rightmost

代码如下：

1：C语言

#include <stdio.h>

int n,tt,f,i,t;

int main()

{

while(scanf("%d",&t)!=EOF)

{

while(t--)

{

scanf("%d",&n);

f=n;

if(n%4==0)

{

n=4;

}

else n%=4;

tt=1;//然后计算f的n次

for(i=0;i<n;i++)

{

tt=tt*f;

}

printf("%d\n",tt);

}

}

return 0;

}

ps：这道题目有规律，题目只要求输出最后一位，所以我们只关注最后一位

1.先来看1的情况，1的n次都为1，所以1的次幂的个位有最小周期1

2.再来看2的情况，2的n次，个位依次位2，4，8，6，2……看到最小周期是4

归纳以下可得任何数的n次幂的个位都有一个周期是4

2：c++

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

long long A(long long a)

{

long long  int t=1,b=a,base=a;

while(b)

{

if(b&1)

t=t*base;

base=base*base;

b/=2;

}

return t;

}

int main()

{

long long a,t;

cin>>t;

while(t--)

{

cin>>a;

cout<<A(a)<<endl;

}

return 0;

}