这种题真的没意思...
字符串加密
http://www.nowcoder.com/questionTerminal/e4af1fe682b54459b2a211df91a91cf3
不是说简单,是说学不到啥新知识,全靠debug,真的烦。
#include <bits/stdc++.h> using namespace std; int main() { string alb("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); string key; string odata; string albtxt; while (getline(cin,key)) { getline(cin, odata); albtxt = alb; // 处理key bool appeard[26]; memset(appeard, 0, sizeof(appeard)); string pkey = key; // key先去重 for (int i = 0; i<pkey.length();) { // 先转成大写字母 pkey[i] = (pkey[i] >= 'a'&&pkey[i] <= 'z') ? pkey[i] - 'a' + 'A' : pkey[i]; if (pkey[i] <= 'Z'&&pkey[i] >= 'A') { if (appeard[pkey[i] - 'A']) { // 如果出现过该字母,删除该字母 pkey.erase(i,1); } else { appeard[pkey[i] - 'A'] = true; i++; } } } // 处理字母表 string temp; for (int i = 0; i<pkey.size();i++) { if (pkey[i] <= 'Z'&&pkey[i] >= 'A') { albtxt[pkey[i] - 'A'] = 127; } } sort(albtxt.begin(), albtxt.end()); albtxt = albtxt.substr(0,unique(albtxt.begin(), albtxt.end())-albtxt.begin()-1); albtxt = pkey + albtxt; // 密码表 // 加密 for (int i = 0; i<odata.size(); i++) { if (odata[i] >= 'A'&&odata[i] <= 'Z') { odata[i] = albtxt[odata[i] - 'A']; } else if (odata[i] >= 'a'&&odata[i] <= 'z') { odata[i] = albtxt[odata[i] - 'a'] - 'A' + 'a'; } } // 输出 cout << odata << endl; odata.clear(); albtxt.clear(); key.clear(); } }