题解 | #复数#
复数
https://www.nowcoder.com/practice/743667f48c4d49a59b7b0613b8aa1c11
#include <iostream> using namespace std; struct Complex { int real, imag; Complex operator+(const Complex &other) { int real = this->real + other.real; int imag = this->imag + other.imag; return Complex{real, imag}; } }; int main() { int m; cin >> m; while (m--) { int real1, imag1, real2, imag2; cin >> real1 >> imag1 >> real2 >> imag2; Complex a{real1, imag1}, b{real2, imag2}; Complex ans = a + b; if (ans.imag > 0) cout << ans.real << '+' << ans.imag << 'i' << endl; else cout << ans.real << ans.imag << 'i' << endl; } return 0; }