蓝桥试题 算法提高 解二元一次方程组 JAVA

问题描述
  给定一个二元一次方程组,形如:
  a * x + b * y = c;
  d * x + e * y = f;
  x,y代表未知数,a, b, c, d, e, f为参数。
  求解x,y
输入格式
  输入包含六个整数: a, b, c, d, e, f;
输出格式
  输出为方程组的解,两个整数x, y。
样例输入
例:
3 7 41 2 1 9
样例输出
例:
2 5
思路:两种解题方法,一种纯暴力,另一种用到了数学的算式。


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt();
		int d = scanner.nextInt();
		int e = scanner.nextInt();
		int f = scanner.nextInt();
		for (int x = 0; x < 1000; x++) {
			for (int y = 0; y < 1000; y++) {
				if (a * x + b * y == c && d * x + e * y == f) {
					System.out.println(x + " " + y);
				}
			}
		}
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt();
		int d = scanner.nextInt();
		int e = scanner.nextInt();
		int f = scanner.nextInt();
		System.out.println((c * e - b * f) / (a * e - b * d) + " "+ (c * d - a * f) / (b * d - a * e));
	}
}

小剧场:1.01的365次方等于37.8,0.99的365次方等于0.03

发布了161 篇原创文章 · 获赞 120 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43771695/article/details/105018232