复数的运算(类和对象)(写的不正规 单纯应付机考)
来源:互联网 发布:淘宝网客服电话是多少 编辑:程序博客网 时间:2024/05/14 05:47
复数的运算(类和对象)
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss
Problem Description
设计一个类Complex,用于封装对复数的下列操作:
成员变量:实部real,虚部image,均为整数变量;
构造方法:无参构造方法、有参构造方法(参数2个)
成员方法:含两个复数的加、减、乘操作。
复数相加举例: (1+2i)+(3+4i)= 4 + 6i
复数相减举例: (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。
Example Input
1 13 4 25 2 12 -1 30 2 2
Example 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-2i
如果输入:
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-2i
Author
zhouxq
import java.util.*;public class Main {public static void main(String[] args) { Scanner cin = new Scanner(System.in); int x = cin.nextInt(); int y = cin.nextInt(); while(cin.hasNext()){ int x1 = cin.nextInt(); int y1 = cin.nextInt(); int op = cin.nextInt(); if(op == 1){ x += x1; y += y1; } else if(op == 2){ x -= x1; y -= y1; } else if(op == 3){ int xx = x; x = x*x1 - y*y1; y = xx*y1 + x1*y; } //System.out.println(x + " " + y); } if(x == 0 && y == 0) System.out.println(0); else if(x == 0 && y == 1) System.out.println("i"); else if(x == 0 && y == -1) System.out.println("-i"); else if(x==0 && y != 0) System.out.println(y+"i"); else if(y==0 && x != 0) System.out.println(x); else if(x != 0 && y < -1) System.out.println(x+""+y+"i"); else if(x != 0 && y == -1) System.out.println(x+"-i"); else if(x != 0 && y == 1) System.out.println(x+"i"); else if(x != 0 && y > 0) System.out.println(x+"+"+y+"i"); }}
0 0
- 复数的运算(类和对象)(写的不正规 单纯应付机考)
- 复数的运算(类和对象)
- 复数的运算(类和对象)
- 复数的运算(类和对象)
- 复数的运算(类和对象)
- 复数类的运算
- 在复数类中自定义类型转换函数实现复数和非复数之间的运算
- js面向对象的复数运算
- 运算符重载(复数运算(+、-、*重载),输入、出类对象(流类运算符>>/<<的重载))
- 复数运算的JAVA类
- C99 标准对复数类型和复数运算的支持
- 复数的加减运算
- 复数的简单运算
- 复数的加减运算
- 复数的运算
- 复数的运算
- 复数的运算法则
- 应付考试用的KMP算法中next数组及nextval数组的计算(笑)
- 运算符、表达式、语句 (预习)
- Spring对JMS的支持
- 面试编程题1
- Django 后台添加含有中文时出现'ascii' codec can't encode characters in position 4-11: ordinal not in range(128)
- You need to use a Theme.AppCompat theme (or descendant) with this activity.
- 复数的运算(类和对象)(写的不正规 单纯应付机考)
- mysql的高并发测试验证
- 机器学习从入门到出家
- Uva.213 Message Decoding
- jquery+css实现自定义对话框功能(不使用插件)
- [hdu2896] 病毒侵袭 AC自动机
- 数据库编程
- UVA 11762 概率dp+期望
- A million requests per second with Python