/*
编写函数:solve,给定浮点数a,b,c,d,e,f,求解方程组ax+by=c,dx+ey=f
任务1:使用assert宏,让解不唯一时退出
任务2:解不唯一时仍然正常返回,但调用者有办法知道解的数量(无解,唯一解,无穷多组解)
思考:函数的参数都有哪些?各是什么类型?
然后请编写一个程序,包含3个函数f().g()和h()。3个函数均无参数,返回值均为int类型
任务1:定义int a,b,要求在依次执行a=f()和b=f()后,a和b的值不同
任务2:定义int a,b,要求在一次执行a=(f() + g()) + h()和b=f() + (g() + h()),a和b的值不同
思路:
矩阵
a b c
d e f
= n(2),有唯一解
小矩阵的挚 = 大矩阵的秩 < n(2),有无穷多解
< ,无解
c b
f e
x =(c*e - b*f)/(a*e - b*d)
a c
d f
y =(a*f - c*d)/(a*e - b*d)
当a*e - b*d != 0时,有唯一解
当a*e - b*d = 0时,若a*f == c*d && c*e == b*f 无穷多解
其他:无解
输入:
3 ,4,5,2,3,5
输出:
-9,8
输入:
1 1 1
2 2 2
输出:
无穷多解
输入:
1 1 1
2 2 1
输出:
无解
*/
/*
关键
1 二元一次方程组解的问题:当a*e - b*d != 0时,有唯一解
当a*e - b*d = 0时,若a*f == c*d && c*e == b*f 无穷多解
其他:无解
*/
/*
关键
1 二元一次方程组解的问题:当a*e - b*d != 0时,有唯一解
当a*e - b*d = 0时,若a*f == c*d && c*e == b*f 无穷多解
其他:无解
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
void solve1(float a,float b,float c,float d,float e,float f)
{
float fDivider = a*e - b*d;
float x,y;
assert(abs(fDivider) > 1e-6);
if(abs(fDivider) > 1e-6)//如果乘积不为0
{
x =(c*e - b*f)/fDivider;
y =(a*f - c*d)/fDivider;
printf("%.2lf %.2lf\n",x,y);
}
}
void solve2(float a,float b,float c,float d,float e,float f)
{
float fDivider = a*e - b*d;
float x,y;
//assert(abs(fDivider) > 1e-6);
if(abs(fDivider) > 1e-6)//如果乘积不为0
{
x =(c*e - b*f)/fDivider;
y =(a*f - c*d)/fDivider;
printf("%.2lf %.2lf\n",x,y);
}
else
{
if(a*f == c*d && c*e == b*f)
{
printf("无穷多解\n");
}
else
{
printf("无解\n");
}
}
}
int main(int argc,char* argv[])
{
float a,b,c,d,e,f;
scanf("%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f);
//solve1(a,b,c,d,e,f);
solve2(a,b,c,d,e,f);
system("pause");
return 0;
}
算法竞赛入门经典: 第四章 函数与递归 4.6解二元一次方程
最新推荐文章于 2022-05-20 18:03:09 发布