inlinestringeraseAll4s(string x){ string ret = ""; int len = x.length(); for (int i = 0; i < len; ++i) if (x[i] == '4') x[i] = '-'; // 删除的'4'用'-'表示 for (int i = 0; i < len; ++i) if (isdigit(x[i])) ret += x[i]; // 采集存留的数字,组成一个新的字符串 return ret; }
inlinestringeraseAll7s(string x){ // 代码思想一样,不再赘述 string ret = ""; int len = x.length(); for (int i = 0; i < len; ++i) if (x[i] == '7') x[i] = '-'; for (int i = 0; i < len; ++i) if (isdigit(x[i])) ret += x[i]; return ret; }
inlinestringeraseAll13s(string x){ string ret = ""; int len = x.length(); for (int i = 0; i < len - 1; ++i) { if (x[i] == '1' && x[i+1] == '3') x[i] = x[i+1] = '-'; // 注意这里要同时检测两个字符 } bool b = false; for (int i = 0; i < len; ++i) if (isdigit(x[i])) ret += x[i]; for (int i = 0; i < len - 1; ++i) { // 重新进行检查 if (ret[i] == '1' && ret[i+1] == '3') { b = true; break; } } if (b) { ret = eraseAll13s(ret); // 递归删除 } return ret; }
stringModify(string x){ string ret = ""; ret = eraseAll4s(x); ret = eraseAll7s(ret); ret = eraseAll13s(ret); // 进行删除 if (ret == "") ret = "0"; return ret; }
intmain(int argc, char *const argv[]){ ios::sync_with_stdio(false); int n; string v; cin >> n; while (n --> 0) { /* 这里是一个比较神奇的 while(), 效果相当于 for (int i = 0; i < n; ++i), 但是会对n进行修改,下标也是从n-1到0 */ cin >> v; cout << Modify(v) << endl; } return0; }