读书人

笔试的二道题目求高人解答

发布时间: 2012-08-11 20:50:31 作者: rapoo

笔试的2道题目,求高人解答
1、设计一个程序,求出712的729次方的最后4位数是多少?
2‘假设A-Z分别为0-25,用来表示二十六进制,则“VMEMOB”转换成八进制是多少?

[解决办法]
第一个题求a的n次方,计算过程中对10000取模。代码如下

C/C++ code
#include<iostream>using namespace std;const int MM=10000;int exp(int a,int n){// (a^n)%MM    int t=1;    while(n){         if(n&1)             t=t*a%MM;         a=(a*a)%MM;         n/=2;    }    return t%MM;}int main(){    int a,n;    while(cin>>a>>n){         cout<<exp(a,n)<<endl;    }    return 0;}
[解决办法]
二分取幂递归写法,避免栈溢出建议采用上面的非递归写法。
C/C++ code
#include<iostream>using namespace std;const int MM=10000;int exp(int a,int n){// (a^n)%MM    int t;    if(n==0)       return 1;    if(n==1)       return a%MM;    t=exp(a,n/2);    t=t*t%MM;    if(n&1)       t=t*a%MM;    return t%MM; }int main(){    int a,n;    while(cin>>a>>n){         cout<<exp(a,n)<<endl;    }    return 0;}
[解决办法]
712的729次方可以直接算出来,我的结果为:2870221156192042420949968632750126516101183204387316291463669036954287379737629805828070218823592011367808657345808237031856106600966651359350349238125138290566094251571859109813674839574262441580263950487271316589537525398492611315240406472457223536960615495403306693353828059993664198431655087160065680810455722997970802403779035389671828770071127841940740630459102789517713229441884432809307550556038916877929052880660846579173103405918474731217835265361288573499381536549255910720036414068466827492165836454006506087241665842632158057303279910257144660233911553633499127532892049792002029304241089942127569568942281310450110451043026600870185077742139393903793950439854689525961484212770766897339451548254670334043746336342931572645664821493876167899972904624109585033462783829166895674437845420647220382458028391727789727022914518834596433989619551429144081540464768796473665267277823656253636581842912236272401513101783889115888266150556409456019379289632177987961304620108925753897999792849742662855319693244768935454731914365629049864320707486002556560175781899466203653590769261617130116624208558681589274524246999049584937251305857989983467210711030205624822390868005217872244248939412855850261733530906120681507318353912419592036297836126671144218482644488525704260103069358027153111535444508842549926411424319087034348711660606167364822915515121838428434164720731531193359142415157667604351078922983079467515699006380007369451608212529811327694785824254744077289130598376638174844568117339495641131394464172268350589582481915313429219219381148383548992191250392317649856028224642482102226018476097167757990169713493091652191975724271057125789556122250820613188440509795538134971929923597823755500816815559241588876633656142769594065591246971027890757710657520626390928380525435491240306977054607596751177895053581089653982025257191928514252756589632181453173060185941488787910589449841909053985483565746762053666644772978867349059228255595019438693553707799183933829398029753160956244556287840808845251990502594988830729204290228384896209580901811150979635517694410752
[解决办法]
1.
C/C++ code
#include <stdio.h>int main(){    long long i,a=712,b=729,c=a;    for(i=0;i<6;i++)        c=(c*c*c)%10000;    printf("%lld\n",c);    return 0;} 

读书人网 >C++

热点推荐