全wa,测试感觉完全没问题,求助///

P1303 A*B Problem

Lss_123 @ 2024-02-22 19:56:19

//A*B
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int max(int x, int y) { return x > y ? x : y; }
vector<int> che(vector<int> a, int x) {//a*x
    if (x == 0) return vector<int>(1, 0);
    reverse(a.begin(), a.end());//个位数在前面
    for (int i = 0,d=0; i < a.size(); i++) {
        int y = x * a[i] + d;
        if (y> 9) 
            if (i == a.size() - 1) { a[i] = y % 10; a.push_back(y / 10); break; }
            else { a[i] = y % 10; d = y / 10; }
        else {
            a[i] = y; d = 0;
        }
    }
    reverse(a.begin(), a.end());//转过来
    return a;
}
vector<int> jia(vector<int> a, vector<int> b) {
    reverse(a.begin(), a.end()); reverse(b.begin(), b.end());//转过来
    int mm = max(a.size(), b.size());
    a.resize(mm, 0); b.resize(mm, 0);
    for (int i = 0; i < mm; i++)
    {
        int x = a[i] + b[i];
        if (x > 9)
            if (i == mm - 1) {
                a.push_back(1); a[i] = x - 10;
            }
            else {
                a[i] = x - 10; a[i + 1]++;
            }
        else a[i] = x;
    }
    reverse(a.begin(), a.end());
    return a;
}
int main() {
    vector<int>A, B,C,D;
    char c;
    while ((c = cin.get())!='\n'&&c!=' ')
        A.push_back(c - '0');
    while ((c = cin.get()) != '\n'&&c!=' ')
        B.push_back(c - '0');
    if (A.size() < B.size()) {
        C = A; A = B; B = C;
    }
    reverse(B.begin(), B.end());
    for (int i = 0; i < B.size(); i++)
    {
        C = che(A, B[i]);
        C.resize(C.size() + i, 0);
        D=jia(C,D);
    }
    for (int i = 0; i < D.size(); i++)
        cout << D[i];
    return 0;

}

by Lss_123 @ 2024-02-22 20:25:03

修改代码:

string S;
cin >> S;
for(int i=0;i<S.size();i++)
    A.push_back(S[i] - '0');
cin >> S;
for (int i = 0; i < S.size(); i++)
    B.push_back(S[i]- '0');

by Orz___zrO @ 2024-02-22 20:28:22

@Lss_123 bkq


by funny1145141919810 @ 2024-02-22 22:27:59

第一眼 看不懂这啥啊这是 第二眼 高精度乘?


by _Persever_ance @ 2024-02-25 19:26:06

@Lss_123 题目里数据不是分两行吗,哪里来的空格


by Lss_123 @ 2024-02-27 11:21:34

@Codelover 如果你自己输入确实是直接分两行,按我之前的代码没问题,但是如果复制它的测试用例调试,就会发现有多余空格


上一页 |