跳转至

高精度

  1. P1601 A+B Problem(高精)

  2. P2142 高精度减法

  3. P1303 A*B Problem

  4. P1760 通天之汉诺塔

  5. P1591 阶乘数码

  6. P1096 [NOIP2007 普及组] Hanoi 双塔问题

  7. P1581 A+B Problem(升级版)

  8. P1604 B进制星球

高精度

A+B Problem(高精)

#include<iostream>
using namespace std;

string add(string a, string b){
    if(a.size() < b.size()) swap(a, b);
    int i = a.size() - 1;
    int j = b.size() - 1;
    while(i > 0){
        if(j >= 0) a[i] += b[j--] - '0';
        if(a[i] > '9'){
            a[i - 1]++;
            a[i] -= 10;
        }
        i--;
    }
    if(a.size() == b.size()){
        a[0] += b[0] - '0'; 
    }
    if(a[0] > '9'){
        a[0] -= 10; 
        a = '1' + a;
    }
    return a;
}

int main(){
    string a, b;
    cin >> a >> b;
    cout << add(a, b);
    return 0;
}
#include<iostream>
using namespace std;

string operator+(string &a, string b){
    if(a.size() < b.size()) swap(a, b);
    int i = a.size() - 1;
    int j = b.size() - 1;
    while(i > 0){
        if(j >= 0) a[i] += b[j--] - '0';
        if(a[i] > '9'){
            a[i - 1]++;
            a[i] -= 10;
        }
        i--;
    }
    if(a.size() == b.size()){
        a[0] += b[0] - '0'; 
    }
    if(a[0] > '9'){
        a[0] -= 10; 
        a = '1' + a;
    }
    return a;
}

int main(){
    string a, b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

c++风格字符串

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
    string s1, s2;
    cin >> s1 >> s2;
    if(s2.size() < s1.size()) swap(s1, s2);
    reverse(s1.begin(), s1.end());
    reverse(s2.begin(), s2.end());
    int i = 0;
//  int len = max(s1.size(), s2.size());
    string s3(s2.size() + 1, '0');
    while(i < s1.size()){
        s3[i] += s1[i] - '0' + s2[i] - '0';
        s3[i + 1] += (s3[i] - '0') / 10;
        s3[i] = (s3[i] - '0') % 10 + '0';
        i++;
    }
    while(i < s2.size()){
        s3[i] += s2[i] - '0';
        s3[i + 1] += (s3[i] - '0') / 10;
        s3[i] = (s3[i] - '0') % 10 + '0';
        i++;
    }
    reverse(s3.begin(), s3.end());
    if(s3[0] == '0') s3 = s3.substr(1);
    cout << s3;
    return 0;
}
高精度加法

  1. P2142 高精度减法

字符数组

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

char a[505];
char b[505];
char c[510];

int main(){
    cin >> a >> b;
    int lena = strlen(a);
    int lenb = strlen(b);
    reverse(a, a + lena);
    reverse(b, b + lenb);
    if(lena > lenb) swap(a, b);
    int i = 0;
    while(i < lena){
        a[i++] -= '0';
    }
    i = 0;
    while(i < lenb){
        c[i] += a[i] + b[i] - '0';
        c[i + 1] = c[i] / 10;
        c[i] = c[i] % 10;
        i++;
    }
    if(c[i] += 0) i += 1;
    while(i--){
        cout << int(c[i]);
    }
    return 0;
}

string

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

void c2i(string a, int size){
    while(size >= 0){
        size--;
        a[size] -= '0';
    }
}

string int_to_char(string a){
    int size = a.size();
    while(size >= 0){
        size--;
        a[size] += '0';
    }
    return a;
}

int main(){
    string a, b;
    cin >> a >> b;
    if(a.size() < b.size() || (a.size() == b.size() && a < b) || a < b){
        swap(a, b);
        cout << "-";
    } 
    string c(a.size(), '0');
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    c2i(a, a.size());
    c2i(b, b.size());
    int i = 0;
    while(i < b.size()){
        if(a[i] < b[i]){
            a[i + 1] -= 1;
            a[i] += 10;
        }
        c[i] = a[i] - b[i];
        i++;
    }
    while(i < a.size()){
        if(a[i] < 0){
            a[i + 1] -= 1;
            a[i] += 10;
        }
        c[i] = a[i] - '0';
        i++;
    }
    reverse(c.begin(), c.end());
    i = 0;
    while(c[i] == 0 && i < c.size() - 1){
        i++;
    }
    string c2 = int_to_char(c.substr(i));
    cout << c2;
    return 0;
}

高精度减法 高精度减法

高精度减法

  1. P1303 A*B Problem

    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;
    int main(){
        string a, b;
        cin >> a >> b;
        int c[a.size() + b.size()] = {0};
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        for(int i = 0; i < a.size(); i++){
            for(int j = 0; j < b.size(); j++){
                c[i + j] += (a[i] - '0') * (b[j] - '0');
                c[i + j + 1] += c[i + j] / 10;
                c[i + j] %= 10;
            }
        }
        int idx = (a.size() + b.size()) - 1;
        while(c[idx] == 0 && idx > 0){
            idx--;
        }
        for(int i = idx; i >= 0; i--){
            cout << c[i];
        }
        return 0;
    }
    

  2. P1760 通天之汉诺塔

  3. P1591 阶乘数码

  4. P1096 [NOIP2007 普及组] Hanoi 双塔问题

  5. P1581 A+B Problem(升级版)

  6. P1604 B进制星球