[Java] 蓝桥杯ADV-96 算法提高 复数求和

版权声明:【https://github.com/liuchuo】大四在校生,水平有限,还望学长们多多包涵,Github真诚求Star~不甚感激!!!(卖萌脸ヾ(=^▽^=)ノ https://blog.csdn.net/liuchuo/article/details/82919324

从键盘读入n个复数(实部和虚部都为整数)用链表存储,遍历链表求出n个复数的和并输出。
样例输入:
3
3 4
5 2
1 3
样例输出:
9+9i
样例输入:
7
1 2
3 4
2 5
1 8
6 4
7 9
3 7
样例输出:
23+39i

package adv96;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int a = 0;
        int b = 0;
        for (int i = 0; i < n; i++) {
            a += in.nextInt();
            b += in.nextInt();
        }
        in.close();
        
        System.out.println(a + "+" + b + "i");
    }

}

猜你喜欢

转载自blog.csdn.net/liuchuo/article/details/82919324