zoukankan      html  css  js  c++  java
  • 蓝桥杯:P1103

    解析: 题目不难,主要是考察复数的四则运算公式,这玩意高中学的早就忘记了,有点坑。另外题目要求自己封装四则运算的方法。

    package _3_5_test;
    
    import java.util.Scanner;
    
    /*P1103
     * */
    public class SeventyOne {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner scanner = new Scanner(System.in);
    
    		String sign = scanner.next();
    		double a = scanner.nextDouble();
    		double b = scanner.nextDouble();
    		double c = scanner.nextDouble();
    		double d = scanner.nextDouble();
    
    		double re[] = null;
    
    		if (sign.equals("+")) {
    			re = add(a, b, c, d);
    		} else if (sign.equals("-")) {
    			re = sub(a, b, c, d);
    		} else if (sign.equals("*")) {
    			re = mul(a, b, c, d);
    		} else if (sign.equals("/")) {
    			re = div(a, b, c, d);
    		}
    
    		System.out.printf("%.2f+%.2fi", re[0], re[1]);
    
    	}
    
    	public static double[] add(Double a, Double b, Double c, Double d) {
    		double result[] = new double[2];
    		result[0] = a + c;
    		result[1] = b + d;
    		return result;
    	}
    
    	public static double[] sub(Double a, Double b, Double c, Double d) {
    		double result[] = new double[2];
    		result[0] = a - c;
    		result[1] = b - d;
    		return result;
    	}
    
    	// 复数的乘法公式:(a+bi)(c+di)=ac+adi+bci-bd
    	public static double[] mul(Double a, Double b, Double c, Double d) {
    		double result[] = new double[2];
    		result[0] = (a * c - b * d);
    		result[1] = (a * d + b * c);
    		return result;
    	}
    
    	// 复数的加法公式:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c*c+d*d)
    	public static double[] div(Double a, Double b, Double c, Double d) {
    		double result[] = new double[2];
    		result[0] = (a * c + b * d) / (c * c + d * d);
    		result[1] = (b * c - a * d) / (c * c + d * d);
    		return result;
    	}
    
    }
    
    
  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1421 搬寝室
    HDU 1176 免费馅饼
    七种排序算法的实现和总结
    算法纲要
    UVa401 回文词
    UVa 10361 Automatic Poetry
    UVa 537 Artificial Intelligence?
    UVa 409 Excuses, Excuses!
    UVa 10878 Decode the tape
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/12633609.html
Copyright © 2011-2022 走看看