Rightmost Digit

xiaoxiao2021-02-28  21

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;

}

转载请注明原文地址: https://www.6miu.com/read-1400233.html

最新回复(0)