zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯 算法训练 p1103

    算法训练 P1103
    时间限制:1.0s 内存限制:256.0MB

    编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:

    要求:(1)定义一个结构体类型来描述复数。
      (2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
      (3)必须使用结构体指针的方法把函数的计算结果返回。
      说明:用户输入:运算符号(+,-,*,/) a b c d.
      输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。

    输入:
      - 2.5 3.6 1.5 4.9
    输出:
      1.00±1.30i

    import java.util.Scanner;
    
    
    public class P1103 {//这道题最大的特点就是注意是double而不是int
    	public static void main(String[] args) {
    		Scanner sc =new Scanner(System.in);
    		String s = sc.next();
    		String [] num = s.split(" ");
    		char aa = num[0].charAt(0);
    //		int a = Integer.valueOf(num[1]);
    //		int b = Integer.valueOf(num[2]);
    //		int c = Integer.valueOf(num[3]);
    //		int d = Integer.valueOf(num[4]);
    		double a = sc.nextDouble();
    		double b = sc.nextDouble();
    		double c = sc.nextDouble();
    		double d = sc.nextDouble();
    		if (aa=='+') {
    			System.out.printf("%.2f",a+c);
    			System.out.print("+");
    			System.out.printf("%.2f",d+b);
    			System.out.print("i");
    		}
    		else if (aa=='-') {
    			System.out.printf("%.2f",a-c);
    			System.out.print("+");
    			System.out.printf("%.2f",b-d);
    			System.out.print("i");
    		}
    		else if (aa=='*') {
    			System.out.printf("%.2f",a*c);
    			System.out.print("+");
    			System.out.printf("%.2f",d*b);
    			System.out.print("i");
    		}
    		else if (aa=='/') {
    			System.out.printf("%.2f",a/c);
    			System.out.print("+");
    			System.out.printf("%.2f",b/d);
    			System.out.print("i");
    		}
    		
    	}
    
    }
    
    
  • 相关阅读:
    二分图最大匹配的König定理及其证明
    HDOJ 2389 Rain on your Parade
    HDOJ 1083 Courses
    HDOJ 2063 过山车
    POJ 1469 COURSES
    UESTC 1817 Complete Building the Houses
    POJ 3464 ACM Computer Factory
    POJ 1459 Power Network
    HDOJ 1532 Drainage Ditches
    HDU 1017 A Mathematical Curiosity
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948701.html
Copyright © 2011-2022 走看看