Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )
Summing complex algorithms to improve the
time limit: 1.0s memory limit: 512.0MB
into n complex read from the keyboard (real and imaginary parts are integers) with a storage chain, n complex traversing the list and obtains and outputs.
Sample input:
. 3
. 3. 4
. 5 2
. 1. 3
Sample Output:
. 9 + 9i
Sample input:
. 7
. 1 2
. 3. 4
2. 5
. 1. 8
. 6. 4
. 7. 9
. 3. 7
Sample Output:
23 is 39i +
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class 复数求和 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
List<Integer> shibu=new LinkedList<Integer>();
List<Integer> xubu=new LinkedList<Integer>();
int n=sc.nextInt();
int i=0;
while(i<n){
shibu.add(sc.nextInt());
xubu.add(sc.nextInt());
i++;
}
int sumofShibu=0;
int sumOfxuBu=0;
for(i=0;i<shibu.size();i++){
sumofShibu+=shibu.get(i);
sumOfxuBu+=xubu.get(i);
}
if(sumOfxuBu>0){
System.out.println(sumofShibu+"+"+sumOfxuBu+"i");
}else{
System.out.println(sumofShibu+""+sumOfxuBu+"i");
}
}
}