1 条题解
-
0
MBC1B 题解
Subtask 1(15分)
此时复数是纯实数,运算退化为实数运算:
- 加法:
- 减法:
- 乘法:
- 除法:(保证 )
#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分):
此时复数是纯虚数 和 :
- 加法:
- 减法:
- 乘法:(实数)
- 除法:(实数)(保证 )
#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
- 上传者