题解 | #复数集合#
复数集合
https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc
#include<cstdio> #include<vector> #include<string> #include<algorithm> using namespace std; bool cmp(string fushu1, string fushu2) { string y3="", y4=""; int y1, y2; int x1, x2; while (fushu1[fushu1.size() - 1]!='i') { y3 = fushu1[fushu1.size() - 1] + y3; fushu1.pop_back(); } fushu1.pop_back();//去掉‘i’ fushu1.pop_back();//去掉‘+’ x1 = stoi(fushu1); y1 = stoi(y3); while (fushu2[fushu2.size() - 1] != 'i') { y4 = fushu2[fushu2.size() - 1] + y4; fushu2.pop_back(); } fushu2.pop_back();//去掉‘i’ fushu2.pop_back();//去掉‘+’ x2 = stoi(fushu2); y2 = stoi(y4); if (x1*x1+y1*y1 < x2*x2+y2*y2) return true; else if (x1 == x2 && y1 > y2) return true; else return false; } int main() { vector<string> fushu; int n; while (scanf("%d", &n) != EOF) { char buffer[50]; string str; for (int i = 0; i <= n; i++) { fgets(buffer, 50, stdin); str = buffer; str.pop_back();//去掉最后的换行符 //str.push_back('$');//加入终止符$ if (str == "Pop" && fushu.empty()) printf("empty\n"); else if (str == "Pop" && !fushu.empty()) { sort(fushu.begin(), fushu.end(),cmp); printf("%s\n", fushu[fushu.size()-1].c_str()); //数组最大元素在最后 fushu.pop_back(); printf("SIZE = %d\n", fushu.size()); } else if (str == "") { continue; } else { int j = str.size() - 1;//指向最后一个字符的位置 string str1=""; while (str[j] != ' ') { str1 = str[j]+ str1; str.pop_back(); j--; } fushu.push_back(str1); printf("SIZE = %d\n", fushu.size()); } } } }