恕个人能力有限,所用测试用例有限,所以可能代码存在问题,如有问题,欢迎提供测试用例,我将及时做修改,非常感谢和大家一起努力学习进步。

题目1:全量字符串的剩余

假设输入一个字符串由全量字符串和已用的字符串组成,其格式为全量字符串@已用字符串。输出在全量字符串减去已用字符串的结果。例如输入a:3,B:5,c:2@B:5,c:1,则输出a:3,c:1。若没有已用字符串,则依旧表示为a:3,B:5,c:2@。对于字母仅限26个英文字母,区分大小写。字符串长度不超过100。

要求:①输出顺序不能改变(按照全量字符串的顺序输出)。②若剩余数目为0,则不输出该字符。

#include <iostream>#include <vector>#include <string>using namespace std;//切出来:和,之间的数字int findNum(string str, int n){ int res = 0; while ((n++) && (n<str.size())) { if (str[n] == ':') continue; if (str[n] == ',') break; if (str[n] == '@') break; if (str[n] >= '0'&&str[n] <= '9') res = res * 10 + (str[n] - '0'); } return res;}//计算的主程序string getResult(string str){ vector<char> qlzifu; vector<int> qlzfnum; vector<char> zyzifu; int i = 0; string res1 = ""; for (; i < str.size(); i++) { if (str[i] == '@') { break; } if ((str[i] >= 'a'&&str[i] <= 'z') || (str[i] >= 'A'&&str[i] <= 'Z')) { qlzifu.push_back(str[i]); } if (str[i] == ':') qlzfnum.push_back(findNum(str, i)); } int numalpha = qlzifu.size(); vector<int> zyzfnum; //处理已占用字符串 if (i != str.size() - 1) { for (; i < str.size(); i++) { if ((str[i] >= 'a'&&str[i] <= 'z') || (str[i] >= 'A'&&str[i] <= 'Z')) { zyzifu.push_back(str[i]); } if (str[i] == ':') zyzfnum.push_back(findNum(str, i)); } } //补充零 int qianque = qlzifu.size() - zyzfnum.size(); while (qianque--) { zyzfnum.push_back(0); } vector<int>res; for (int m = 0; m < qlzifu.size(); m++) { for (int zyi = 0; zyi < zyzifu.size(); zyi++) { if (qlzifu[m] == zyzifu[zyi]) res.push_back(qlzfnum[m] - zyzfnum[zyi]); } res.push_back(qlzfnum[m]); } for (int no = 0; no < qlzifu.size(); no++) { if (res[no] <= 0) continue; string stmp(1, qlzifu[no]); res1.append(stmp); res1.append(":"); res1.append(to_string(res[no])); if (no < qlzifu.size() - 1) res1.append(","); } return res1;}int main() { string str; string res = getResult(str); cout << res << endl; return 0;}

题目2:省略

(未做出,待补充)

题目3:逻辑表达式

(待补充)