9.4 华为笔试题2下来理了理思路(当时没处理好输入真蛋疼)

#include <iostream>
#include <regex>
#include <map>
using namespace std;

int main() {
int n;
cin >> n;
char tmp[256];
cin.ignore(256, '\n');
map<string, int>m;
while (n--) {
cin.getline(tmp,256,'\n');
string data(tmp);
regex reg(" ");
string deal = regex_replace(data, reg, "");
regex reg1("[+=]");
sregex_token_iterator test(deal.begin(), deal.end(), reg1, {-1});
sregex_token_iterator end;
int index = 1;
string now;
int total = 0;
for (; test != end; ++test) {
if (index == 1) {
++index;
now = test->str();
}
else {
if (*test <= '9'&&*test >= '0') {
if (total == -10000) {
break;
}else
total += stoi(test->str());
}
else {
string s_(test->str());
if (m.count(s_)==0) {
total = -10000;
break;
}
else {
total += m[s_];
}

}
++index;

}


}
if (index == 2)
m[now] = -10000; //避免没赋值 xx= 这种情况
else
m[now] = total;

if (n == 0) {
if (m[now] == -10000)
cout << "NA";
else
cout << m[now];


}
}
return 0;

}
#华为##笔试题目#
全部评论

相关推荐

1 9 评论
分享
牛客网
牛客企业服务