Rumah > Soal Jawab > teks badan
例如输入a3,解码后就是aaa;输入goo12le就是gooooooooooooole;输入XXX为结束。鄙人新手,点都不会.....麻烦来点简单的...初学者,不会高深的语法。最好是循环解决的....
三叔2016-11-05 11:42:10
auto decode = [](string origin) { vectortarget; for (int pos, i = 0; i < origin.length(); ) { if (origin[i] >= 'a'&&origin[i] <= 'z') { target.push_back(origin[i]); pos = i++; } else if (origin[i] >= '0'&&origin[i] <= '9') { int count = 0; while (origin[i] >= '0'&&origin[i] <= '9') { count *= 10; count+=origin[i++] - '0'; } do { target.push_back(origin[pos]); } while (--count); } //if have other char else i++; } // last : vector -> string char *buffer = new char[target.size()+1]; for (int i = 0; i < target.size(); i++) buffer[i] = target[i]; buffer[target.size()] = '\0'; auto result = string(buffer); delete[] buffer; return result; };
记录1个pos就行了。