高精度例题

喜你入骨 提交于 2019-12-03 07:19:16

1168:大整数加法

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N],b[N],c[N],k,maxlen;
void read(int arr[],int len){
    char s[N];
    scanf("%s",s);
    len = strlen(s);
    maxlen = max(maxlen,len);
    int go = 0;
    while(arr[go] == '0' && go < len) go++;
    for(int i = go;i < len ; ++i) arr[len - i - 1] = s[i] - '0';
}
void print(int arr[]){
    while(arr[maxlen] == 0 && maxlen > 1) --maxlen;
    for(int i = maxlen;i >= 0; --i) printf("%d",arr[i]);
    puts("");
}
void add(int a[],int b[],int c[],int len){
    for(int i = 0;i < len; ++i){
        c[i] += a[i] + b[i];
        if(c[i] >= 10){
            c[i + 1] ++;
            c[i] -= 10;
        }
    }
}
int main(){
    read(a,maxlen);
    read(b,maxlen);
    add(a,b,c,maxlen);
    print(c);
    return 0;
}

1169:大整数减法

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N],b[N],c[N],k,lena,lenb;
void read(int arr[],int &len){
    char s[N];
    scanf("%s",s);
    len = strlen(s);
    int go = 0;
    while(arr[go] == '0' && go < len) go++;
    for(int i = go;i < len ; ++i) arr[len - i - 1] = s[i] - '0';
}
void print(int arr[],int len){
    while(arr[len] == 0 && len > 0) --len;
    for(int i = len;i >= 0; --i) printf("%d",arr[i]);
    puts("");
}
void sub(int a[],int b[],int c[],int lena,int lenb){
    int len = max(lena,lenb);
    for(int i = 0;i < len; ++i){
        c[i] += a[i] - b[i];
        if(c[i] < 0){
            c[i + 1] --;
            c[i] += 10;
        }
    }
}
bool f(int a[],int b[],int lena,int lenb){
    if(lena > lenb) return 1;
    else if(lena < lenb) return 0;
    else {
        for(int i = 0;i < lena; ++i){
            if(a[i] > b[i]) return 1;
            else if(b[i] > a[i]) return 0;
        }
    }
    return 1;
}
int main(){
    read(a,lena);
    read(b,lenb);
    //add(a,b,c,lena,lenb);
    if(f(a,b,lena,lenb)) sub(a,b,c,lena,lenb);
    else {
        putchar('-');
        sub(b,a,c,lenb,lena);
    }
    print(c,max(lena,lenb));
    return 0;
}

1170:计算2的N次方

#include <bits/stdc++.h>
using namespace std;
const int N = 2e4;
int a[N],k = 1;
int main(){
    int n,s;
    scanf("%d",&n);
    a[1] = 1;
    for(int i = 1;i <= n; ++i){
        for(int j = 1;j <= k; ++j) a[j] <<= 1;
        for(int j = 1;j <= k; ++j){
            s = 1;
            if(a[j] >= 10){
                a[j+1] += a[j]/10;
                a[j] %= 10;
                s = max(j+1,s);
            }
        }
        k = max(s,k);
    }
    for(int i = k;i >= 1; --i) printf("%d",a[i]);
    return 0;
}

1307:【例1.3】高精度乘法

#include <bits/stdc++.h>
using namespace std;
const int N = 200;
int a[N],b[N],c[N*2],lena,lenb;
void read(int a[],int &len){
    char arr[N];
    scanf("%s",arr);
    int length = strlen(arr),go = 0;
    while(arr[go] == '0' && go < length) go++;
    for(int i = go;i < length; ++i) a[length - i - 1] = arr[i] - '0';
    len = length - go;
}
void print(int a[],int len){
    while(a[len] == 0 && len > 0) len--;
    for(int i = len;i >= 0; --i) printf("%d",a[i]);
    puts("");
}
// 这种乘法参考 https://oi-wiki.org/math/bignum/
void mul(int a[],int b[],int c[],int len){
    for(int i = 0;i < len; ++i){
        for(int j = 0;j <= i; ++j) c[i] += a[j] * b[i - j];
        if(c[i] >= 10){
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
    }
}
int main(){
    read(a,lena);
    read(b,lenb);
    mul(a,b,c,lena+lenb);
    print(c,lena+lenb);
    return 0;
}

1308:【例1.5】高精除

#include <bits/stdc++.h>
using namespace std;
const int N = 200;
int a[N],b[N],c[N],d,i;
void init(int a[]){
    char s[N];
    scanf("%s",s);
    int len = strlen(s);
    a[0] = len;
    for(int i = 1;i <= a[0]; ++i) a[i] = s[a[0] - i] - '0';
}
void print(int a[]){
    if(a[0] == 0) {
        printf("0\n");
        return ;
    }
    for(int i = a[0];i > 0; --i) printf("%d",a[i]);
    puts("");
}
int compare(int a[],int b[]){
    if(a[0] > b[0]) return 1;
    if(a[0] < b[0]) return -1;
    for(int i = a[0];i > 0; --i){
        if(a[i] > b[i]) return 1;
        if(a[i] < b[i]) return -1;
    }
    return 0;
}
void jian(int a[],int b[]){
    int flag = compare(a,b);
    if(flag == 0) {
        a[0] = 0;
        return ;
    }
    if(flag == 1){
        for(int i = 1;i <= a[0]; ++i){
            if(a[i] < b[i]){
                a[i + 1] --;
                a[i] += 10;
            }
            a[i] -= b[i];
        }
        while(a[0] > 0 && a[a[0]] == 0) a[0] --;
        return ;
    }
}
void numcpy(int p[],int q[],int det){
    for(int i = 1;i <= p[0];++ i) q[i + det - 1] = p[i];
    q[0] = p[0] + det - 1;
}
void div(int a[],int b[],int c[]){
    int tmp[N];
    c[0] = a[0] - b[0] + 1;
    for(int i = c[0];i > 0; -- i){
        memset(tmp,0,sizeof(tmp));
        numcpy(b,tmp,i);
        while(compare(a,tmp) >= 0) {
            c[i]++;
            jian(a,tmp);
        }
    }
    while(c[0] > 0 && c[c[0]] == 0) c[0] --;
}

int main(){
    init(a);
    init(b);
    div(a,b,c);
    print(c);
    print(a);
    return 0;
}

1309:【例1.6】回文数(Noip1999)

#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n,a[N],lena,b[N];
void read(int a[],int &lena){
    char s[N];
    scanf("%s",s);
    lena = strlen(s);
    int go = 0;
    while(s[go] == '0' && go < lena) go++;
    for(int i = go;i < lena; ++i) {
        if(s[i] >= '0' && s[i] <= '9') a[lena - i - 1] = s[i] - '0';
        else a[lena - i - 1] = s[i] - 'A' + 10;
    }
    lena -= go;
}
bool check(int a[]){
    for(int i = 0;i < lena/2; ++i){
        if(a[i] != a[lena - i - 1]) return 0;
    }
    return 1;
}
void addself(int a[]){
    for(int i = lena-1;i >= 0; --i) b[lena-i-1] = a[i];
    for(int i = 0;i < lena; ++i){
        a[i] += b[i];
        a[i+1] += a[i]/n;
        a[i] %= n;
    }
    if(a[lena]) lena++;
}
void print(int a[]){
    for(int i = lena-1;i >= 0; --i) printf("%d",a[i]);
    puts("");
}
int main(){
    int step = 0;
    scanf("%d",&n);
    read(a,lena);
    for(int i = 1;i <= 30; ++i){
        if(check(a)){
            printf("%d",step);
            return 0;
        }
        else {
            addself(a);
            //print(a);
            step ++;
        }
    }
    printf("Impossible");
    return 0;
}

1171:大整数的因子

#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int a[N],len;
void read(int a[],int &len){
    char s[N];
    scanf("%s",s);
    len = strlen(s);
    int go = 0;
    while(s[go] == '0' && go < len) go++;
    for(int i = go;i < len; ++i) a[len-i-1] = s[i] - '0';
    len -= go;
}
void print(int a[]){
    for(int i = len-1;i >= 0; --i) printf("%d",a[i]);
    puts("");
}
bool div(int x){
    int res = 0,i = len-1;
    while(i >= 0){
        res *= 10;
        res += a[i];
        res %= x;
        --i;
    }
    if(res == 0) return 1;
    else return 0;
}
int main(){
    read(a,len);
    bool flag = 1;
    for(int i = 2;i <= 9; ++i){
        if(div(i)){
            if(flag) printf("%d",i);
            else printf(" %d",i);
            flag = 0;
        }
    }
    if(flag) puts("none");
    return 0;
}

1172:求10000以内n的阶乘

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N],len = 1,n,x;
int main(){
    scanf("%d",&n);
    a[1] = 1;
    for(int i = 2;i <= n; ++i){
        x = 0;
        for(int j = 1;j <= len; ++j){
            a[j] = a[j] * i + x;
            x = a[j] / 10;
            a[j] %= 10;
            if(x > 0 && j == len) len++;
        }
    }
    for(int i = len;i >= 1; --i) printf("%d",a[i]);
    return 0;
}

1173:阶乘和

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n,a[N],lena = 1,lenb,b[N];
inline void fac(int n){
    memset(b,0,sizeof b);
    int x;
    b[1] = 1;
    lenb = 1;
    for(int i = 2;i <= n; ++i){
        x = 0;
        for(int j = 1;j <= lenb; ++j){
            b[j] = b[j] * i + x;
            x = b[j] / 10;
            b[j] %= 10;
            if(x > 0 && j == lenb) lenb++;
        }
    }
}
inline void add(){
    lena = max(lena,lenb);
    for(int i = 1;i <= lena; ++i){
        a[i] += b[i];
        if(a[i] >= 10){
            a[i + 1] += a[i] / 10;
            a[i] %= 10;
        }
    }
    if(a[lena+1]) lena ++;
}
int main(){
    scanf("%d",&n);
    a[1] = 1;
    for(int i = 2;i <= n; ++i){
        fac(i);
        add();
    }
    for(int i = lena;i >= 1; --i) printf("%d",a[i]);
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!