1 条题解

  • 0
    @ 2026-1-16 12:42:21

    MBC1B 题解

    Subtask 1(15分)

    此时复数是纯实数,运算退化为实数运算:

    • 加法:a1+a2a_1 + a_2
    • 减法:a1−a2a_1 - a_2
    • 乘法:a1×a2a_1 \times a_2
    • 除法:a1/a2a_1 / a_2(保证 a2≠0a_2 \neq 0)
    #include <cstdio>
    
    int main() {
        int n;
        scanf("%d", &n);
        while (n--) {
            int a1, b1, a2, b2, opt;
            scanf("%d%d%d%d%d", &a1, &b1, &a2, &b2, &opt);
            // b1 = b2 = 0 的情况
            double real_res = 0, imag_res = 0;
            
            if (opt == 1) real_res = a1 + a2;
            else if (opt == 2) real_res = a1 - a2;
            else if (opt == 3) real_res = a1 * a2;
            else if (opt == 4) real_res = (double)a1 / a2;
            
            printf("%.3f %.3f\n", real_res, imag_res);
        }
        return 0;
    }
    

    Subtask 2(15分):

    此时复数是纯虚数 b1ib_1i 和 b2ib_2i:

    • 加法:(b1+b2)i(b_1 + b_2)i
    • 减法:(b1−b2)i(b_1 - b_2)i
    • 乘法:−b1b2-b_1b_2(实数)
    • 除法:b1/b2b_1 / b_2(实数)(保证 b2≠0b_2 \neq 0)
    #include <cstdio>
    
    int main() {
        int n;
        scanf("%d", &n);
        while (n--) {
            int a1, b1, a2, b2, opt;
            scanf("%d%d%d%d%d", &a1, &b1, &a2, &b2, &opt);
            // a1 = a2 = 0 的情况
            double real_res = 0, imag_res = 0;
            
            if (opt == 1) imag_res = b1 + b2;
            else if (opt == 2) imag_res = b1 - b2;
            else if (opt == 3) real_res = -b1 * b2;
            else if (opt == 4) real_res = (double)b1 / b2;
            
            printf("%.3f %.3f\n", real_res, imag_res);
        }
        return 0;
    }
    

    Subtask 3(10分)

    加减法最简单,不需要处理乘法或除法的复数运算:

    #include <cstdio>
    
    int main() {
        int n;
        scanf("%d", &n);
        while (n--) {
            int a1, b1, a2, b2, opt;
            scanf("%d%d%d%d%d", &a1, &b1, &a2, &b2, &opt);
            
            double real_res, imag_res;
            if (opt == 1) {  // 加法
                real_res = a1 + a2;
                imag_res = b1 + b2;
            } else {  // opt == 2,减法
                real_res = a1 - a2;
                imag_res = b1 - b2;
            }
            
            printf("%.3f %.3f\n", real_res, imag_res);
        }
        return 0;
    }
    

    Subtask 4(20分)

    没有除法时,只需要实现加、减、乘三种运算:

    #include <cstdio>
    
    int main() {
        int n;
        scanf("%d", &n);
        while (n--) {
            int a1, b1, a2, b2, opt;
            scanf("%d%d%d%d%d", &a1, &b1, &a2, &b2, &opt);
            
            double real_res, imag_res;
            if (opt == 1) {  // 加法
                real_res = a1 + a2;
                imag_res = b1 + b2;
            } else if (opt == 2) {  // 减法
                real_res = a1 - a2;
                imag_res = b1 - b2;
            } else if (opt == 3) {  // 乘法
                real_res = a1 * a2 - b1 * b2;
                imag_res = a1 * b2 + b1 * a2;
            }
            
            printf("%.3f %.3f\n", real_res, imag_res);
        }
        return 0}
    

    Subtask 5(40分)

    需要实现所有四种运算,注意除法公式和类型转换:

    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    int main() {
        int n;
        scanf("%d", &n);
        
        for (int i = 0; i < n; i++) {
            int a1, b1, a2, b2, opt;
            scanf("%d %d %d %d %d", &a1, &b1, &a2, &b2, &opt);
            
            double real_res, imag_res;
            
            switch (opt) {
                case 1: // 加法
                    real_res = a1 + a2;
                    imag_res = b1 + b2;
                    break;
                case 2: // 减法
                    real_res = a1 - a2;
                    imag_res = b1 - b2;
                    break;
                case 3: // 乘法
                    real_res = a1 * a2 - b1 * b2;
                    imag_res = a1 * b2 + b1 * a2;
                    break;
                case 4: // 除法
                    {
                        // 注意使用double防止溢出
                        double den = (double)a2 * a2 + (double)b2 * b2;
                        real_res = (a1 * a2 + b1 * b2) / den;
                        imag_res = (b1 * a2 - a1 * b2) / den;
                    }
                    break;
            }
            
            printf("%.3f %.3f\n", real_res, imag_res);
        }
        
        return 0;
    }
    

    另外,还可以使用 C++ 的 complex 类。

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	int n;
    	cin>>n;
    	while(n--){
    		int a1,b1,a2,b2,opt;
    		cin>>a1>>b1>>a2>>b2>>opt;
    		complex<double> x(a1,b1),y(a2,b2);
    		if(opt==1) x+=y;
    		if(opt==2) x-=y;
    		if(opt==3) x*=y;
    		if(opt==4) x/=y;
    		cout<<fixed<<setprecision(3)<<x.real()<<" "<<x.imag()<<'\n';
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    30
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    389
    已通过
    77
    上传者