查找兄弟单词(HJ27)
一:解题思路
二:完整代码示例 (C++版和Java版)
C++代码:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; bool isBrother(string a, string b) { if (a.size() == b.size()) { if (a == b) return false; sort(a.begin(),a.end()); sort(b.begin(),b.end()); if (a == b) return true; } return false; } int main() { int num = 0; while (cin >> num) { string str = ""; string word = ""; string s = ""; int index = -1; vector<string> vs; for (int i = 0; i < num; i++) { cin >> str; vs.push_back(str); } sort(vs.begin(), vs.end()); cin >> word; cin >> index; int counts = 0; for (int i = 0; i < vs.size(); i++) { if (isBrother(vs[i], word)) { counts++; if (counts == index) s = vs[i]; } } if (!vs.empty()) cout << counts << endl; if (counts >= index) cout << s << endl; } return 0; }

浙公网安备 33010602011771号