U278408 的某些思路
PekingDuck · · 个人记录
题目链接
题目大意:用程序解二元一次方程组
输入格式:方程组的增广矩阵
输出格式:方程的解
整体思路
克莱姆法则:
若有二元一次方程组:
其中
若记
则方程的解可以写为
那我们就可以利用它用除法解方程
但是
以上针对的是二元非齐次线性方程组
所以我们要对 二元齐次线性方程组 特殊对待 即 二元齐次线性方程组 的零解 和 非零解
我们知道如果 D = 0 那么此 二元齐次线性方程组有非零解
理论存在 实践开始
#include <cstdio>
using namespace std;
int x1, y1, n1, x2, y2, n2;
int D1, D2, D;
int answerx, answery;
void inp( ) {
scanf( "%d%d%d%d%d%d", &x1, &y1, &n1, &x2, &y2, &n2 ); //输入方程组的增广矩阵
}
void work( ) {
if( n1 == n2 and n2 == 0 ) // 判断二元齐次线性方程组 的零解 和 非零解
if( D != 0 ) { // 零解
printf( "x = 0 \ny = 0 " );
return;
}
D = x1 * y2 - x2 * y1;
D1 = n1 * y2 - n2 * y1;
D2 = x1 * n2 - x2 * n1;
answerx = D1 / D; //x1
answery = D2 / D; //x2
}
void outp( ) {
printf( "x = %d \ny = %d ", answerx, answery );
}
int main( void ) {
inp();
work();
outp();
return 0;
}