蓝桥杯算法训练 P1103 结构体指针or 暴力

  算法训练 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                                                              

思路:被这个要求忽悠了,我自己写了一个暴力算的也满分过了啊,,,我还以为必须要用结构体指针。。。
就写了两种
#include<bits/stdc++.h>
using namespace std;
struct node{
	double a,b;
};
node *add(node x,node y)
{
	node *w=(node *)malloc(sizeof(node));
	w->a=x.a+y.a;
	w->b=x.b+y.b;
	return w; 
}
node *sub(node x,node y)
{
	node *w=(node *)malloc(sizeof(node));
	w->a=x.a-y.a;
	w->b=x.b-y.b;
	return w; 
}
node *mul(node x,node y)
{
	node *w=(node *)malloc(sizeof(node));
	w->a=x.a*y.a-x.b*y.b;
	w->b=x.a*y.b+y.a*x.b;
	return w; 
}
node *div(node x,node y)
{
	node *w=(node *)malloc(sizeof(node));
	w->a=(x.a*y.a+x.b*y.b)/(y.a*y.a+y.b*y.b);
	w->b=(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b);
	return w; 
}
int main()
{
	char c;
	struct node n,m;
	struct node *s;
	scanf("%c %lf %lf %lf %lf",&c,&n.a,&n.b,&m.a,&m.b);
	switch(c)
	{
		case '+':
			s=add(n,m);
			break;
		case '-': 
			s=sub(n,m);
			break;
		case '*':
			s=mul(n,m);
			break;
		case '/':
			s=div(n,m); 
	 } 
	 printf("%.2lf+%.2lfi",s->a,s->b);
	 return 0;
}


#include<stdio.h>  
int main()  
{  
    char ch;  
    double a,b,c,d;  
    scanf("%c%lf%lf%lf%lf",&ch,&a,&b,&c,&d);  
    if(ch=='+')  
    {  
        printf("%.2lf+%.2lfi",a+c,b+d);  
    }  
    if(ch=='-')  
    {  
        printf("%.2lf+%.2lfi",a-c,b-d);  
    }  
    if(ch=='*')  
    {  
        printf("%.2lf+%.2lfi",a*c-b*d,a*d+b*c);  
    }  
    if(ch=='/')  
    {  
        printf("%.2lf+%.2lfi",(a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));  
    }  
    return 0;  
}  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marcus-Bao

万水千山总是情,只给五角行不行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值