Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Complex number multiplication (leetcode)
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { //1+-2i public static String complexNumberMultiply(String a, String b) { int num1,num2; int coe1,coe2; if(a.substring(0,1).equals("-")){ int index = a.indexOf('+'); num1 = Integer.parseInt(a.substring(1,index))*-1; if(a.substring(index+1,index+2).equals("-")) coe1 = Integer.parseInt(a.substring(index+2,a.indexOf('i')))* -1; else coe1 = Integer.parseInt(a.substring(index+1,a.indexOf('i'))); } else{ int index = a.indexOf('+'); num1 = Integer.parseInt(a.substring(0,index)); if(a.substring(index+1,index+2).equals("-")) coe1 = Integer.parseInt(a.substring(index+2,a.indexOf('i')))* -1; else coe1 = Integer.parseInt(a.substring(index+1,a.indexOf('i')));//78+-76i } if(b.substring(0,1).equals("-")){ int index = b.indexOf('+'); num2 = Integer.parseInt(b.substring(1,index))*-1; if(b.substring(index+1,index+2).equals("-")) coe2 = Integer.parseInt(b.substring(index+2,b.indexOf('i')))* -1; else coe2 = Integer.parseInt(b.substring(index+1,b.indexOf('i'))); } else{ int index = b.indexOf('+'); num2 = Integer.parseInt(b.substring(0,index)); if(b.substring(index+1,index+2).equals("-")) coe2 = Integer.parseInt(b.substring(index+2,b.indexOf('i')))* -1; else coe2 = Integer.parseInt(b.substring(index+1,b.indexOf('i')));//78+-76i } int resNum = (num1*num2)-(coe1*coe2); int resCoe = (num1*coe2)+(num2*coe1); return new String(resNum+"+"+resCoe+"i"); } public static void main(String args[]) { System.out.println("Hello, World!"); System.out.println(complexNumberMultiply("20+22i","-18+-10i")); } }
run
|
edit
|
history
|
help
0
3.D
Preference List
OBF-7 PR
combination sum (variation)
test no.1
Split
Fireball Plugin
assigment java programming
program to count number of lines
Qu1java.java