P1303 A*B Problem
P1303 A*B Problem
题目
给出两个非负整数,求它们的乘积。
输入
输入共两行,每行一个非负整数。
输出
输出一个非负整数表示乘积。
样例
输入
1
2
输出
2
提示
每个非负整数不超过
思路
根据题意,乘数的数据最大范围是
设两个乘数分别为
其中:
做完乘法后可以总结出规律:乘积第
for (int i = 1; i <= lena; i ++ )
{
x = 0;
for (int j = 1; j <= lenb; j ++ )
{
x = a[i] * b[j] + x / 10 + c[i + j - 1];
c[i + j - 1] = x % 10;
}
c[i + lenb] = x / 10;
}
lenc = lena + lenb;
while (c[lenc] == 0 && lenc > 1) // 去掉多余的前导 0
lenc --;
代码一
#include <bits/stdc++.h>
using namespace std;
char a1[2010], b1[2010];
int a[2010], b[2010], c[4000010], lena, lenb, lenc, x;
void read()
{
scanf("%s %s", a1, b1);
lena = strlen(a1);
lenb = strlen(b1);
for (int i = 0; i <= lena - 1; i ++ )
a[lena - i] = a1[i] - 48;
for (int i = 0; i <= lenb - 1; i ++ )
b[lenb - i] = b1[i] - 48;
}
void mul()
{
for (int i = 1; i <= lena; i ++ )
{
x = 0;
for (int j = 1; j <= lenb; j ++ )
{
x = a[i] * b[j] + x / 10 + c[i + j - 1];
c[i + j - 1] = x % 10;
}
c[i + lenb] = x / 10;
}
lenc = lena + lenb;
while (c[lenc] == 0 && lenc > 1) // 去掉前导 0
lenc --;
}
void print()
{
for (int i = lenc; i >= 1; i -- )
cout << c[i];
}
int main()
{
read();
mul();
print();
return 0;
}
代码二:重载运算符
#include <bits/stdc++.h>
using namespace std;
char str[2010];
struct node
{
int len, s[4010];
node()
{
len = 0;
memset(s, 0, sizeof(s));
}
};
node operator * (const node &a, const node &b)
{
node c;
c.len = a.len + b.len - 1;
if ((a.len == 1 && a.s[1] == 0) || (b.len == 1 && b.s[1] == 0))
{
c.len = 1;
c.s[1] = 0;
return c;
}
for (int i = 1; i <= a.len; i ++ )
{
for (int j = 1; j <= b.len; j ++ )
{
c.s[i + j - 1] += a.s[i] * b.s[j];
c.s[i + j] += c.s[i + j - 1] / 10;
c.s[i + j - 1] %= 10;
}
}
if (c.s[c.len + 1])
{
int x = c.s[c.len + 1], len = c.len;
while (x)
{
c.s[++ len] = x % 10;
x /= 10;
}
c.len = len;
}
return c;
}
node read()
{
scanf("%s", str);
int len = strlen(str);
node a;
a.len = len;
for (int i = 0; i < len; i ++ )
a.s[len - i] = str[i] - '0';
return a;
}
void print(node a)
{
for (int i = a.len; i >= 1; i -- )
printf("%d", a.s[i]);
}
int main()
{
node a, b, c;
a = read();
b = read();
c = a * b;
print(c);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现