zoukankan      html  css  js  c++  java
  • 蓝桥杯 算法训练 ALGO-142 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
     
    题目解析:
      复数运算法则:
        (1)加法运算
            (a+bi) + (c+di) = (a+c) + (b+d)i
        (2)减法运算
             (a+bi) - (c+di) = (a-c) + (b-d)i
        (3)乘法运算
            (a+bi) * (c+di) = (ac-bd) + (bc+ad)i
        (4)除法运算
            (a+bi) / (c+di) = (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i
     
    示例代码:
     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 import java.text.DecimalFormat;
     5 
     6 public class Main {
     7     private static double[] num;
     8     private static BufferedReader br;
     9     private static DecimalFormat df;
    10     
    11     public static void main(String[] args) throws IOException {
    12         br = new BufferedReader(new InputStreamReader(System.in));
    13         df=new DecimalFormat("0.00");
    14         String[] str = br.readLine().split(" ");
    15 
    16         char op= str[0].charAt(0);
    17         
    18         num = new double[str.length-1];
    19         for(int i = 0; i < num.length; i++){
    20             num[i] = Double.parseDouble(str[i+1]);
    21         }
    22         
    23         complexCalculation(op,num);
    24         
    25     }
    26     
    27     /**
    28      * 复数运算
    29      * @param op    运算符号
    30      * @param num   a b c d
    31      */
    32     private static void complexCalculation(char op, double[] num){
    33         if(op == '+'){
    34             System.out.println(df.format(num[0] + num[2])+ "+" +df.format(num[1] + num[3])+ "i");
    35         }else if(op == '-'){
    36             System.out.println(df.format(num[0] - num[2])+ "+" +df.format(num[1] - num[3])+ "i");
    37         }else if(op == '*'){
    38             System.out.println(df.format(num[0]*  num[2] - num[1] * num[3])+ "+" 
    39                     +df.format(num[0] * num[3] + num[1] * num[2])+ "i");
    40         }else if(op == '/'){
    41             System.out.println(df.format((num[0] * num[2] + num[1] * num[3]) / (num[3] * num[3] + num[2] * num[2]))+ "+"
    42                     +df.format((num[1] * num[2] - num[0] * num[3]) / (num[3] * num[3] + num[2] * num[2])) +"i");
    43         }
    44     }
    45 }
  • 相关阅读:
    如何不传入对象就获得某对象的方法---ThreadLocal类
    Linux系统主目录被更改,怎么修改回去?
    tree命令的安装
    Linux命令学习man
    当重载函数的参数是Object和Object数组的时候会发生什么情况!!!
    Linux学习(二)之内核、系统调用、库
    使用puttygen转换OpenSSH SSH2私钥为RSA PEM格式
    docker-compose使用详解
    svn迁移到gitlab
    linux快速启动http端口
  • 原文地址:https://www.cnblogs.com/cao-lei/p/6653629.html
Copyright © 2011-2022 走看看