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 QuQ_ @ 2024-02-22 19:57:21
@Lss_123 python 一行就够了
by QuQ_ @ 2024-02-22 19:57:52
@Lss_123
print(int(input())*int(input()))
python这一行就过了……
by Lss_123 @ 2024-02-22 19:59:55
@QuQ_ 不会python啊,大佬可以看看我这个c++的那里有问题吗?
by Orz___zrO @ 2024-02-22 20:00:08
@Lss_123 要不你输一个
1 2
by Orz___zrO @ 2024-02-22 20:00:42
@Lss_123 你输一下
1 2
这个输出都错了
by Lss_123 @ 2024-02-22 20:03:33
@Orz___zrO 啥意思啊,我输入 1 2 得到的:2 没错啊 不是A*B吗 难道我理解错了5555
by Orz___zrO @ 2024-02-22 20:05:09
@Lss_123 我是sb,看错了,以为是A+B
by Lss_123 @ 2024-02-22 20:06:38
@Orz___zrO 求求看看孩子的代码吧,无论我怎么测试,感觉都没问题
by Orz___zrO @ 2024-02-22 20:11:05
@Lss_123 不过我感觉你用vector写的有些长,用char数组存更好写。你这个我看不出来
#include <bits/stdc++.h>
using namespace std;
const int N=10095;
int a[N],b[N],c[N*2];
string sa,sb;
int la,lb;
int main(){
cin>>sa>>sb;
la=sa.size();
lb=sb.size();
for(int i=0;i<la;i++){
a[la-i]=sa[i]-'0';
}
for(int i=0;i<lb;i++){
b[lb-i]=sb[i]-'0';
}
for(int i=1;i<=la;i++){
for(int j=1;j<=lb;j++){
c[i+j-1]+=a[i]*b[j];
c[i+j]+=c[i+j-1]/10;
c[i+j-1]%=10;
}
}
int l=la+lb;
while(c[l]==0 && l>1) l--;
for(int i=l;i>=1;i--) cout<<c[i];
}
by Lss_123 @ 2024-02-22 20:23:41
@Orz___zrO 我知道我哪里错了,就是存的时候,可能用get.cin()函数会把输入流里的空格吃掉,看了你的代码我就改用string读入然后再存,就AC了!!!我这种用vector的写很多函数的确实没有你简洁,太感谢了!!