题解 | #复数#

复数

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;
}

全部评论

相关推荐

牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务