救命,哪错了!!

P1303 A*B Problem

Jeff_赵 @ 2023-07-17 16:47:46

#include<bits/stdc++.h>
#include<string>
using namespace std;

const int N=1e3+10;
int s[N*N]={0};
char a[N*N],b[N*N];

int main()
{
    cin>>a>>b;
    int tot=0,tot1=0,i=strlen(a)-1,j=strlen(b)-1;
    while(i>=0)
    {
        int sw1=a[i]-'0';
        tot=tot1;
        while(j>=0)
        {

            int sw2=b[j]-'0';
            int dan=sw2*sw1;
            if(s[tot]+dan<10)
            {
                s[tot]+=dan;
            }
            else
            {
                s[tot]+=(dan+s[tot])%10;
                s[++tot]+=(dan+s[tot-1])/10;
            }
            tot++;
            j--;
        }
        j=strlen(b)-1;
        i--;
        tot1++;
    }
    for(int i=tot-1;i>=0;i--)
    {
        printf("%d",s[i]);
    }
    return 0;
} 

粗略估计错在34--36行,但我不知道如何改?求救!!


by forest1145141919810 @ 2023-08-16 20:35:36

@Jeff_赵 大概看了一下,蒟蒻觉得是你把计算结果存在数组里的时候写错了


by forest1145141919810 @ 2023-08-16 20:36:19

@Jeff_赵 如果你像我这样写,错误的概率就会大大减小

#include<iostream>
using namespace std;
#define N 10005
struct Num { int num[N], cnt, f; };
inline void read(int x[], int& cnt, int& f) {//读入高精度数
    cnt = 0; f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-') f = -f; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x[++cnt] = ch - 48; ch = getchar(); }
    int lin[N] = {}; for (int i = cnt; i >= 1; i--) lin[i] = x[cnt - i + 1];
    for (int i = 1; i <= cnt; i++) x[i] = lin[i];
}
inline void write(int x[], int cnt, int f) {//输出高精度数
    if (f == -1 && !(cnt == 1 && x[cnt] == 0)) putchar('-'), f = -f;
    for (int i = cnt; i >= 1; i--) putchar(x[i] + '0');
    putchar('\n'); return;
}
inline void copy(int x1[], int cnt1, int f1, int x2[], int& cnt2, int& f2) {//把高精度数x1完全复制给高精度数ans
    for (int i = 1; i <= cnt1; i++) x2[i] = x1[i];
    cnt2 = cnt1; f2 = f1;
}
inline void getmax(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2, int ans[], int& cntans, int& fans) {//获取最大值,存储到数组ans中
    if (f1 > f2) { copy(x1, cnt1, f1, ans, cntans, fans); return; }
    else if (cnt1 > cnt2) { copy(x1, cnt1, f1, ans, cntans, fans); return; }
    else if (f1 < f2) { copy(x2, cnt2, f2, ans, cntans, fans); return; }
    else if (cnt1 < cnt2) { copy(x2, cnt2, f2, ans, cntans, fans); return; }
    bool ismax = true;
    for (int i = cnt1; i >= 1; i--) if (x1[i] < x2[i]) { ismax = false; break; }
    if(ismax) { copy(x1, cnt1, f1, ans, cntans, fans); return; }
    else { copy(x2, cnt2, f2, ans, cntans, fans); return; }
}
inline void getmin(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2, int ans[], int& cntans, int& fans) {//获取最小值,存储到数组ans中
    if (f1 < f2) { copy(x1, cnt1, f1, ans, cntans, fans); return; }
    else if (cnt1 < cnt2) { copy(x1, cnt1, f1, ans, cntans, fans); return; }
    else if (f1 > f2) { copy(x2, cnt2, f2, ans, cntans, fans); return; }
    else if (cnt1 > cnt2) { copy(x2, cnt2, f2, ans, cntans, fans); return; }
    bool ismax = true;
    for (int i = cnt1; i >= 1; i--) if (x1[i] > x2[i]) { ismax = false; break; }
    if (ismax) { copy(x1, cnt1, f1, ans, cntans, fans); return; }
    else { copy(x2, cnt2, f2, ans, cntans, fans); return; }
}
inline bool ismax(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2) {//判断数x1是不是更大
    if (f1 > f2) return true;
    else if (cnt1 > cnt2) return true;
    else if (f1 < f2) return false;
    else if (cnt1 < cnt2) return false;
    bool ismax = true;
    for (int i = cnt1; i >= 1; i--) if (x1[i] < x2[i]) return false;
    return true;
}
inline bool ismin(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2) {//判断数x1是不是更小
    if (f1 > f2) return false;
    else if (cnt1 > cnt2) return false;
    else if (f1 < f2) return true;
    else if (cnt1 < cnt2) return true;
    bool ismax = true;
    for (int i = cnt1; i >= 1; i--) if (x1[i] > x2[i]) return false;
    return true;
}
inline void add(int x1[], int cnt1, int x2[], int cnt2, int ans[], int &cntans) {//高精度加法,不考虑符号
    int len = max(cnt1, cnt2); int x = 0;
    for (int i = 1; i <= len; i++) {
        ans[i] = (x1[i] + x2[i] + x) % 10;
        x = (x1[i] + x2[i] + x) / 10;
    }if (x > 0) ans[++len] = x;
    cntans = len;
}
inline void _minus(int x1[], int cnt1, int x2[], int cnt2, int ans[], int &cntans,int &f) {//高精度减法,不考虑符号
    if (ismin(x1, cnt1, 1, x2, cnt2, 1)) {
        copy(x2, cnt2, 1, ans, cntans, f); f = -f;
        for (int i = cnt1; i >= 1; i--) {
            if (ans[i] < x1[i]) {
                int cnt = i + 1; ans[i] = ans[i] + 10 - x1[i];
                while (ans[cnt] == 0 && cnt <= cntans) ans[cnt] = 9, cnt++;
                ans[cnt]--; while (ans[cntans] == 0) cntans--;
            }
            else ans[i] -= x1[i];
        }while (cntans > 0 && ans[cntans] == 0) cntans--; if (cntans == 0) cntans++;
    }
    else {
        copy(x1, cnt1, 1, ans, cntans, f);
        for (int i = cnt2; i >= 1; i--) {
            if (ans[i] < x2[i]) {
                int cnt = i + 1; ans[i] = ans[i] + 10 - x2[i];
                while (ans[cnt] == 0 && cnt <= cntans) ans[cnt] = 9, cnt++;
                ans[cnt]--; while (ans[cntans] == 0) cntans--;
            }
            else ans[i] -= x2[i];
        }while (ans[cntans] == 0) cntans--; if (cntans == 0) cntans++;
    }
}
inline void multiply(int x1[], int cnt1, int x2[], int cnt2, int ans[], int& cnt) {
    for (int i = 1; i <= cnt1; i++) {
        int temp = 0;
        for (int j = 1; j <= cnt2; j++) {
            ans[i + j - 1] = ans[i + j - 1] + x1[i] * x2[j] + temp;
            temp = ans[i + j - 1] / 10; ans[i + j - 1] %= 10;
        }ans[i + cnt2] += temp;
    }
    cnt = cnt1 + cnt2;
    while (ans[cnt] == 0 && cnt > 0) cnt--;
    if (cnt == 0) cnt = 1;
}
inline void Init(Num &a) { a.cnt = 0; }
inline void Read(Num &a) { read(a.num, a.cnt, a.f); }
inline void Write(Num a) { write(a.num, a.cnt, a.f); }
inline void Copy(Num a, Num &b) { copy(a.num, a.cnt, a.f, b.num, b.cnt, b.f); }//复制
inline void GetMax(Num a, Num b, Num &c) { getmax(a.num, a.cnt, a.f, b.num, b.cnt, b.f, c.num, c.cnt, c.f); }//获取最大值
inline void Getmin(Num a,Num b,Num &c){ getmin(a.num, a.cnt, a.f, b.num, b.cnt, b.f, c.num, c.cnt, c.f); }//获取最小值
inline bool IsMax(Num a, Num b) { return ismax(a.num, a.cnt, a.f, b.num, b.cnt, b.f); }//判断a是不是最大
inline bool IsMin(Num a, Num b) { return ismin(a.num, a.cnt, a.f, b.num, b.cnt, b.f); }//判断a是不是最小
inline void Add(Num a, Num b, Num &c) {//有符号高精度加法
    if (a.f < 0 && b.f>0) _minus(b.num, b.cnt, a.num, a.cnt, c.num, c.cnt, c.f);
    else if (a.f > 0 && b.f < 0) _minus(a.num, a.cnt, b.num, b.cnt, c.num, c.cnt, c.f);
    else { c.f = a.f; add(a.num, a.cnt, b.num, b.cnt, c.num, c.cnt); }
}
inline void Minus(Num a, Num b, Num &c) {//有符号高精度减法
    if (b.f < 0 && a.f>0) { c.f = 1; add(a.num, a.cnt, b.num, b.cnt, c.num, c.cnt); }
    else if (a.f < 0 && b.f>0) { c.f = -1; add(a.num, a.cnt, b.num, b.cnt, c.num, c.cnt); }
    else if (a.f > 0 && b.f > 0) _minus(a.num, a.cnt, b.num, b.cnt, c.num, c.cnt, c.f);
    else if (a.f < 0 && b.f < 0) _minus(b.num, b.cnt, a.num, a.cnt, c.num, c.cnt, c.f);
}
inline void Multiply(Num a, Num b, Num& c) { c.f = a.f * b.f; multiply(a.num, a.cnt, b.num, b.cnt, c.num, c.cnt); }
Num a, b, c, d, e;
inline void work() {    
    Init(a), Init(b), Init(c), Init(d), Init(e);
    Read(a), Read(b);
    Multiply(a, b, e);
    Write(e);

}
signed main() { work(); return 0; }

|