import java.util.*; class Complex{ int real, image; Complex() { } public Complex(int real, int image) { this.real = real; this.image = image; } void cal(int r, int i, int a) { if(a == 1) { this.real += r; this.image += i; } else if(a == 2) { this.real -= r; this.image -= i; } else if(a == 3) { int re = this.real; int im = this.image; this.real = re * r - im*i; this.image = re*i + r*im; } } void out(){ if(real == 0&&image==0) { System.out.println("0"); } else if(real ==0 && image != 0){ if(image == 1) { System.out.println("i"); } else if(image == -1) { System.out.println("-i"); } else { System.out.println(image+"i"); } } else if(real != 0 && image == 0) { System.out.println(real); } else { if(image == 1) { System.out.println(real+"+i"); } else if(image == -1) { System.out.println(real+"-i"); } else if(image > 1){ System.out.println(real+"+"+image+"i"); } else { System.out.println(real+""+image+"i"); } } } } public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int x,y,a; x = cin.nextInt(); y = cin.nextInt(); Complex complex = new Complex(x, y); while(cin.hasNext()) { x = cin.nextInt(); y = cin.nextInt(); a = cin.nextInt(); complex.cal(x, y, a); } complex.out(); } }
复数的运算(类和对象)
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
复数相减举例: (1+2i)-(3+4i)= -2 - 2i
复数相乘举例: (1+2i)*(3+4i)= -5 + 10i
要求:对复数进行连环运算。
Input
输入有多行。
第一行有两个整数,代表复数X的实部和虚部。
后续各行的第一个和第二个数表示复数Y的实部和虚部,第三个数表示操作符op: 1——复数X和Y相加;2——复数X和Y相减;3——复数X和Y相乘。
Output
计算数据输出其简化复数形式,如:-2-2i、-4、-3i、1+2i、0。
Sample Input
1 1 3 4 2 5 2 1 2 -1 3 0 2 2
Sample Output
5-7i
Hint
输入与输出形式示例:
如果输入:
2 3
-2 1 1
则输出: 4i
如果输入:
1 2
-1 -2 1
则输出: 0
复数的输出形式示例:
实部 虚部 输出形式
0 0 0
-4 0 -4
0 4 4i
3 2 3+2i
3 -2 3-2
如果输入:
2 3
-2 1 1
则输出: 4i
如果输入:
1 2
-1 -2 1
则输出: 0
复数的输出形式示例:
实部 虚部 输出形式
0 0 0
-4 0 -4
0 4 4i
3 2 3+2i
3 -2 3-2