9.4华为笔试第二题32位(用大数相加)C++代码求大佬查看

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
using namespace std;
bool isnum(char c)
{
	if (c >= '0' && c <= '9')
		return true;
	else
		return false;
}
//字符串去空格
char *strremov(char* dst, const char* src, char ch = ' ')
{
	int i = -1, j = 0;
	while (src[++i])
	if (src[i] != ch)
		dst[j++] = src[i];
	dst[j] = '\0';
	return dst;
}
//含+号 分割每个数
void splitstring(const string& srcstr, vector<string>& vec, const string& separator)
{
	string::size_type possubstringstart;
	string::size_type posseparator;
	posseparator = srcstr.find(separator);
	possubstringstart = 0;
	while (string::npos != posseparator)
	{
		vec.push_back(srcstr.substr(possubstringstart, posseparator - possubstringstart));
		possubstringstart = posseparator + separator.size();
		posseparator = srcstr.find(separator, possubstringstart);
	}
	if (possubstringstart != srcstr.length())
		vec.push_back(srcstr.substr(possubstringstart));
}
//字符串 两数相加
string BigNumAdd(const string& strNum1, const string& strNum2)
{
	string strSum;
	int len1 = strNum1.size();
	int len2 = strNum2.size();
	int bit = 0;                
	while (len1 > 0 || len2 > 0 || bit)
	{
		int tmpSum = (len1 ? strNum1[len1 - 1] - '0' : 0) + (len2 ? strNum2[len2 - 1] - '0' : 0) + bit;
		strSum += tmpSum % 10 + '0'; 
		bit = tmpSum / 10;
		len1 = len1 ? len1 - 1 : 0;
		len2 = len2 ? len2 - 1 : 0;
	}
	reverse(strSum.begin(), strSum.end());
	return strSum;
}

int main() 
{
	int n = 0, m = 0;
	vector<string>res;
	cin >> n;
	cin.ignore();
	vector<string> input;
	map<string, string> mpint;
	vector<string> jiashu;
	for (int i = 0; i < n; i++)
	{
		char c[100];
		string tmp;
		getline(cin, tmp, '\n');
		strremov(c, tmp.c_str());
		tmp = c;
		input.push_back(tmp);
	}
	for (int i = 0; i < n; i++)
	{
		string::size_type position;
		int len = input[i].size();
		position = input[i].find("=");
		string subs1, subs2, re = "0";
		subs1 = input[i].substr(0, position);
		subs2 = input[i].substr(position + 1, len);
		if (subs2.find("+") != subs2.npos)	//有+
		{
			vector<string> jiashu;
			splitstring(subs2, jiashu, "+");
			int jslen = jiashu.size();
			for (int j = 0; j < jslen; j++)
			{
				//加数不是数字 ,到map中找对应的val string
				if (!isnum(jiashu[j][0]))
				{
					map<string, string>::iterator it = mpint.find(jiashu[j]);
					if (it == mpint.end())
					{
						cout << "NA";
						return 0;
					}
					else {
						re = BigNumAdd(re, it->second);
					}
				}
				else
				{
					re = BigNumAdd(re, jiashu[j]);
				}
				if (j == jslen - 1){
					mpint.insert(pair<string, string>(subs1, re));
					res.push_back(re);
				}
			}
		}
		//没有+
		else
		{
			if (isnum(input[i][position + 1]))
			{
				mpint.insert(pair<string, string>(subs1, subs2));
				res.push_back(subs2);
			}
			else if (!isnum(input[i][position + 1]))
			{
				map<string, string>::iterator it = mpint.find(subs2);
				if (it == mpint.end())
				{
					cout << "NA";
					return 0;
				}
				else
				{
					mpint.insert(pair<string, string>(subs1, it->second));
					res.push_back(subs2);
				}
			 }
			}
	}
	cout << res[n - 1];
	system("pause");
	return 0;
}
/*
输入1:
4
xx    =111111111
yy=    xx+111111111
zz=xx+yy+111111111
tt=xx+yy+zz+111111111
输出:888888888

输入1:
4
xx    =111111111
yy=    xx+111111111
zz=xx+yy+vv
tt=xx+yy+zz+111111111
输出:NA
*/

#华为##笔试题目##C/C++#
全部评论
请问楼主华为笔试是用牛客还是赛码呢,输入是不是要自己处理呢
点赞 回复 分享
发布于 2019-09-06 22:11
***。华为笔试题要写那么多的么
点赞 回复 分享
发布于 2019-09-05 22:32

相关推荐

马上要带我人生中的第一个实习生了,想问问大家都喜欢什么的mentor?好让我有个努力的目标
拒绝996的劳伦斯很勇敢:看得见目标且护犊子的 具体就是明确告诉组员要干什么,然后当别的组甩dirty work时能护的组自家新人
点赞 评论 收藏
分享
求面试求offer啊啊啊啊:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
10
分享

创作者周榜

更多
牛客网
牛客企业服务