GDUFE ACM-1010

本文介绍了一道关于计算幂次运算结果末位数的简单算法题目。通过观察数字规律,采用暴力方法解决该问题,针对不同数字的末位数循环特性进行分类讨论,并提供了一段C语言实现代码。

题目:http://acm.gdufe.edu.cn/Problem/read/id/1010

 

A hard puzzle(简单题)

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

        GOJ gives a hard puzzle to C_Shit_Hu, super13, TestN,LOP: gave a and b,how to know the a^b.Everybody objects to this BT problem,so GOJ makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input:

There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output:

For each test case, you should output the a^b's last digit number.

Sample Input:

7 66
8 800

Sample Output:

9
6

思路:找规律,0 1 5 6每次的末位数都是自己本身,4 9是两次一循环,剩下的都是四次一循环,我直接用b%4看看应该用哪个末位数……超级暴力==

难度:如果像我这样取巧。。。还是比较简单的,但是。。还是用正常办法比较好,使用快速幂,我还没有学会,有时间再去看看。。

代码:
 1 #include<stdio.h>
 2 int main()
 3 {
 4     long long int a,b,c,i;
 5     while(~scanf("%lld %lld",&a,&b))
 6     {
 7         c=1;
 8         if(b%4==0)
 9             c=a*a*a*a;
10         else for(i=0;i<(b%4);i++)
11         {
12             c=c*a;
13         }
14         c=c%10;
15         printf("%lld\n",c);
16     }
17     return 0;
18 }

 

转载于:https://www.cnblogs.com/ruo786828164/p/6005132.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值