牵着气球的路人 2019-09-29 21:19 采纳率: 100%
浏览 508

如何实现1^1+2^2+……20^20?

求教:如何用java语言实现1^1+2^2+……20^20,要求不能用大数BigInteger;不能用Math;不能使用实型数;不能溢出;要求使用数组,每个数组元素只存放一位;使用循环控制。
有没有大佬能帮忙看一看啊……c++也可以。谢谢各位大佬!

  • 写回答

3条回答 默认 最新

  • JonathanYan 2019-09-29 22:14
    关注

    每个数组元素只存放一位 是什么意思,数组的第i位存放i的i次方么,还是存放结果的第i位。
    20的20次方能到27位数,已经超过了long long int能表示的最大值。
    不是不能用大数据是不能用大数(BigIntenger)吧,意思就是自己实现一个?

    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    #define max(a,b) (((a)>(b))?(a):(b))
    #define min(a,b) (((a)<(b))?(a):(b))
    
    class BigInt{
    private:
        char *list;
        int length;
    public:
        BigInt(int num);
        BigInt(string str);
        BigInt(const BigInt& t);
        ~BigInt();
    
        BigInt operator+(const BigInt& t);
        BigInt operator+=(const BigInt& t);
        BigInt operator*(const BigInt& t);
        BigInt operator*=(const BigInt& t);
        BigInt operator=(const BigInt& t);
        BigInt operator=(int t);
    
        friend ostream& operator<<(ostream& s,const BigInt& t);
    }; 
    
    BigInt::BigInt(int len = 1){
        if(len < 1)
            len = 1;
        length = len;
        list = new char[length];
        for(int i = 0; i < length; i++)
            list[i] = 0;
    }
    
    BigInt::BigInt(string str){
        length = str.length();
        list = new char[length];
        for(int i = 0; i < length; i++)
            list[i] = str[length - i - 1] - '0';
    }
    
    BigInt::BigInt(const BigInt& t){
        length = t.length;
        list = new char[length];
        for(int i = 0; i < length; i++)
            list[i] = t.list[i];
    //  cout << "Copy :" << endl;
    //  cout << "  From :" << t << endl;
    //  cout << "  To   :" << *this << endl;
    }
    
    BigInt::~BigInt(){
    //  cout << "Destroy : ";
    //  cout << *this << endl;
    //  system("pause"); 
        delete[] list;
    }
    
    BigInt BigInt::operator+(const BigInt& t){
        int l1 = (list[length-1] ? length : length - 1);
        int l2 = (t.list[t.length-1] ? t.length : t.length - 1);
    
        BigInt temp(max(l1, l2) + 1);
        for( int i = 0; i < max(l1, l2); i++){
            if( i < length )
                temp.list[i] += list[i]; 
            if( i < t.length )
                temp.list[i] += t.list[i];
            temp.list[i+1] += temp.list[i] / 10;
            temp.list[i] %= 10;
        }
        return temp;
    }
    
    BigInt BigInt::operator+=(const BigInt& t){
        *this = *this + t;
        return *this;
    }
    
    BigInt BigInt::operator*(const BigInt& t){
        int l1 = (list[length-1] ? length : length - 1);
        int l2 = (t.list[t.length-1] ? t.length : t.length - 1);
    //  cout << "l " << l1 << " " << l2 << endl;
        BigInt temp(l1 + l2);
    
        int sum = 0;
        for(int i = 0; i < l1 + l2; i++){
            for(int j = max(0, i - l2 + 1); j < l1 && j <= i; j++){
                sum += list[j] * t.list[i - j];
            }
            temp.list[i] = (sum % 10);
    //      cout << "[" << i << "] = " << sum << ", temp[" << i << "] = " << (int)temp.list[i] << endl;
            sum /= 10;
        }
    
        return temp;
    }
    
    BigInt BigInt::operator*=(const BigInt& t){
        *this = *this * t;
        return *this;
    }
    
    BigInt BigInt::operator=(const BigInt& t){
        delete[] list;
        length = t.length;
        list = new char[t.length];
        for(int i = 0; i < length; i++)
            list[i] = t.list[i];
    }
    
    BigInt BigInt::operator=(int t){
        int len = 0, n = t;
        while(n){
            len++;
            n/=10;
        }
    
        if(len<1) 
            len = 1;
        length = len;
    
        delete[] list;
        list = new char[length];
    
        for(int i = 0; i < length; i++){
            list[i] = t % 10;
            t /= 10;
        }
        return *this;
    }
    
    ostream& operator<<(ostream &out,const BigInt& t){
        int len = (t.list[t.length-1] ? t.length : t.length - 1);
        for(int i = len - 1; i >= 0; i--)
            out << (int)t.list[i];
        return out;
    }
    
    int main(){
        BigInt sum, a, b;
        for(int i = 1; i <= 20; i++ ){
            a = i, b = i;
            for( int j = 1; j < i; j++)
                a = a * b;
            cout << i << "^" << i << " = " << a << endl;
            sum += a;
        }
    
        cout << "sum = " << sum << endl;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法