• 人见人爱A^B


    Description

    求A^B的最后三位数表示的整数。 说明:A^B的含义是“A的B次方”
     

    Input

    输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
     

    Output

    对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
     

    Sample Input

    2 3 12 6 6789 10000 0 0
     

    Sample Output

    8 984 1
     
    水题一道
    通过递归来简化次方的预算,要点是每层递归都要取其最后三位数,并且要注意b的奇偶性
    附上源代码
     1 #include <iostream>
     2 using namespace std;
     3 
     4 int power( int a, int b );
     5 int main (){
     6 
     7     int A,B;
     8     cin>>A>>B;
     9     while ( A!=0 && B!=0 )
    10     {
    11         cout<<power(A,B) % 1000<<endl;
    12         cin>>A>>B;
    13     }
    14 }
    15 int power( int a, int b ){
    16     if ( b == 3 ) 
    17         { a = a%1000;return a*a*a; }
    18     else if ( b == 2 )
    19         { a = a%1000;return a*a; }
    20 
    21     if ( b%2 == 1 )
    22         return (power(a,b/2) % 1000) * (power(a,b/2+1) % 1000);
    23     else 
    24         return (power(a,b/2) % 1000) * (power(a,b/2) % 1000);
    25 }
  • 相关阅读:
    使用servicename连接Oracle数据库
    使用SID连接Oracle数据库
    使用xlrd模块
    【Project Euler 8】Largest product in a series
    Git使用帮助
    【Project Euler 1】Multiples of 3 and 5
    VSCode使用新体验
    导出牛顿引力形式为平方反比的两种方式
    NOIP2018游记
    即将退役声明
  • 原文地址:https://www.cnblogs.com/neverchanje/p/3451574.html
Copyright © 2020-2023  润新知