【蓝桥杯】A*B 问题

题目

计算两个非负整数 <math> <semantics> <mrow> <mi> A </mi> <mo separator="true"> , </mo> <mi> B </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> A,B </annotation> </semantics> </math>A,B 的乘积, <math> <semantics> <mrow> <mi> A </mi> <mo separator="true"> , </mo> <mi> B </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> A,B </annotation> </semantics> </math>A,B 可能会很大。

输入格式
第一行输入一个非负整数 <math> <semantics> <mrow> <mi> A </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> A </annotation> </semantics> </math>A
第二行输入一个非负整数 <math> <semantics> <mrow> <mi> B </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> B </annotation> </semantics> </math>B
<math> <semantics> <mrow> <mi> A </mi> <mo separator="true"> , </mo> <mi> B </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> A,B </annotation> </semantics> </math>A,B 的长度不大于 <math> <semantics> <mrow> <mn> 500 </mn> </mrow> <annotation encoding="application&#47;x&#45;tex"> 500 </annotation> </semantics> </math>500

输出格式
输出 <math> <semantics> <mrow> <mi> A </mi> <mo> × </mo> <mi> B </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> A\times B </annotation> </semantics> </math>A×B 的值。

样例输入

4321

1234

样例输出

5332114

题解

先转置,再运算

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
	string A,B;
	int ReverseA[1000+5];
	int ReverseB[1000+5];
	int a[1000+5];
	memset(a,0,sizeof(a));
	cin>>A>>B;
	int lenA = A.length();
	int lenB = B.length();
	// 得到逆转数组 
	for(int i=0;i<lenA;i++)
		ReverseA[i] = A[lenA-i-1]-'0';
	for(int i=0;i<lenB;i++)
		ReverseB[i] = B[lenB-i-1]-'0';
	// 计算 
	for(int i=0;i<lenA;i++)
		for(int j=0;j<lenB;j++){
			a[i+j] += ReverseA[i]*ReverseB[j];
			// 处理进位 
			if(a[i+j]>=10){
				a[i+j+1] += a[i+j]/10;
				a[i+j] %= 10;
			}
		}
	// 控制输出 
	bool flag = false;
	for(int i=lenA+lenB;i>=0;i--){
		if(a[i] && !flag || i==0)  // 答案为 0 的特殊情况 
			flag = true;
		else if(!a[i] && !flag)
			continue;	
		cout<<a[i];
	}
	return 0;
}

返回目录,查看更多

全部评论

相关推荐

喜欢喜欢喜欢:这是我见过最长最臭的简历
点赞 评论 收藏
分享
吐泡泡的咸鱼:我也工作了几年了,也陆陆续续面试过不少人,就简历来说,第一眼学历不太够,你只能靠你的实习或者论文或者项目经历,然后你没有论文,没有含金量高的比赛和奖项,只能看实习和项目,实习来说,你写的实习经历完全不清楚你想找什么工作?行研?数据分析?且写的太少了,再看项目,这些项目先不说上过大学读过研究生的都知道很水,然后对你想找的岗位有什么帮助呢?项目和实习也完全不匹配啊,你好像在努力将你所有的经历都放在简历里想表现你的优秀,但是对于你想找的岗位来说,有什么用呢?最后只能获得岗位不匹配的评价。所以你需要明白你想要找的岗位要求是什么,是做什么的,比如产品经理,然后再看你的经历里有什么匹配的上这个岗位,或者对这个岗位以及这个岗位所在的公司有价值,再写到你的简历上
点赞 评论 收藏
分享
05-24 20:52
东南大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务