A^B(A的B次方)——人见人爱A^B,Rightmost Digit 。

写一个pow()函数计算A的B次方取余10,即求所得结果的个位数。

int pow(int a, int b)
{
  if(b==0)
    return 1;
  int half = (pow(a,b>>1))%10;
  if(b%2==0)
    return (half*half)%10;
  else
    return (half*half*a)%10;
}

可用于

Rightmost Digit

 

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pow(int x, int n)
{
  if(n==0)
    return 1;
  int half = (pow(x,n>>1))%10;
  if(n%2==0)
    return (half*half)%10;
  else
    return (half*half*x)%10;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,p;
scanf("%d",&n);
p=n;
n=n%10;
if(n==0||n==1||n==5||n==6)
{
printf("%d\n",n);
continue;
}
int sum=(int)pow(n,p);
printf("%d\n",sum);
}
return 0;
}


修改下函数还可用于

人见人爱A^B

 

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pow(int x, int n)
{
  if(n==0)
    return 1;
  int half = (pow(x,n>>1))%1000;
  if(n%2==0)
    return (half*half)%1000;
  else
    return (half*half*x)%1000;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m),n||m)
{
n=n%1000;
int sum=pow(n,m);
printf("%d\n",sum);
}
return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值