• 15-算法训练 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
     
    思路:
    首先在java里面用类代替结构体,注意不要用public class, 且将成员变量设置为public便于使用,否则要写set,get函数,很麻烦;
    格式输出注意:用system.out.printf()或者String.format()都可以,注意double 是%f 而不是lf.
    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner cin = new Scanner(System.in);
    		char fh = cin.next().charAt(0); // 注意这里不用nextLine
    		fushu t1 = new fushu();
    		t1.shi = cin.nextDouble();
    		t1.xu = cin.nextDouble();
    		fushu t2 = new fushu();
    		t2.shi = cin.nextDouble();
    		t2.xu = cin.nextDouble();
    		if(fh == '+') {
    			t1.add(t2);
    		}
    		else if(fh == '-'){
    			t1.jian(t2);
    		}
    		else if(fh == '*') {
    			t1.chen(t2);
    		}
    		else {
    			t1.chu(t2);
    		}
    		
    //		System.out.println(t1.shi + "+" + t1.xu + "i");
    //		System.out.println(String.format("%.2f+%.2fi", t1.shi, t1.xu));
    		System.out.printf("%.2f+%.2fi", t1.shi, t1.xu);
    	}
    
    }
    
    class fushu{
    	public double shi;
    	public double xu;
    	fushu(){
    		
    	}
    	fushu(double a, double b){
    		shi = a;
    		xu = b;
    	}
    	public void jian(fushu b) {
    		this.shi -= b.shi;
    		this.xu -= b.xu;
    	}
    	public void add(fushu b) {
    		this.shi += b.shi;
    		this.xu += b.xu;
    	}
    	public void chen(fushu b) {
    		double shi = this.shi;
    		double xu = this.xu;
    		this.shi = shi * b.shi - xu * b.xu;
    		this.xu = shi * b.xu + xu * b.shi;
    	}
    	public void chu(fushu b) {
    		double shi = this.shi;
    		double xu = this.xu;
    		this.shi = (shi * b.shi + xu * b.xu) / (b.shi * b.shi + b.xu * b.xu);
    		this.xu = (xu * b.shi - shi * b.xu) / (b.shi * b.shi + b.xu * b.xu);
    	}
    }
    

      

  • 相关阅读:
    UVALive 7456 Least Crucial Node (并查集)
    UVALive 7454 Parentheses (栈+模拟)
    HDU 5778 abs (枚举)
    HDU 5776 sum (模拟)
    HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)
    HDU 5805 NanoApe Loves Sequence (模拟)
    HDU 5734 Acperience (推导)
    HDU 5842 Lweb and String (水题)
    HDU 5833 Zhu and 772002 (高斯消元)
    HDU 5835 Danganronpa (贪心)
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/10367262.html
Copyright © 2020-2023  润新知