1051 复数乘法 (15 分)
复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i2=−1;也可以写成极坐标下的指数形式 (R×e(Pi)),其中 R 是复数模,P 是辐角,i 是虚数单位,其等价于三角形式 R(cos(P)+isin(P))。
现给定两个复数的 R 和 P,要求输出两数乘积的常规形式。
输入格式:
输入在一行中依次给出两个复数的 R1, P1, R2, P2,数字间以空格分隔。
输出格式:
在一行中按照 A+Bi
的格式输出两数乘积的常规形式,实部和虚部均保留 2 位小数。注意:如果 B
是负数,则应该写成 A-|B|i
的形式。
输入样例:
2.3 3.5 5.2 0.4
结尾无空行
输出样例:
-8.68-8.23i
结尾无空行
#include<bits/stdc++.h>
using namespace std;
int main() {
double r1,p1,r2,p2;
cin>>r1>>p1>>r2>>p2;
double t1=r1*cos(p1)*r2*cos(p2)-r1*sin(p1)*r2*sin(p2);
double t2=r1*sin(p1)*r2*cos(p2)+r1*cos(p1)*r2*sin(p2);
if(fabs(t1)<0.005)
t1=0.00;
if(fabs(t2)<0.005)
t2=0.00;
cout<<fixed<<setprecision(2)<<t1;
if(t2<0) cout<<"-"<<fixed<<setprecision(2)<<-t2<<"i";
else cout<<"+"<<fixed<<setprecision(2)<<t2<<"i";
return 0;
}
1001 A+B Format (20 分)
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
结尾无空行
Sample Output:
-999,991
结尾无空行
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
string s=to_string(a+b);
for(int i=0;i<s.length();i++){
cout<<s[i];
if(s[i]!='-'&&(i+1)%3==s.length()%3&&i!=s.length()-1) cout<<",";
}
return 0;
}