ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อค้นหาตัวเลขสองตัว (พูดว่า 'a' และ 'b') เพื่อให้ทั้งคู่
a+b = N and a*b = N are satisfied.
การขจัด 'a' ออกจากสมการทั้งสอง เราได้สมการกำลังสองใน 'b' และ 'N' เช่น
b2 - bN + N = 0
สมการนี้จะมีรากสองรากซึ่งจะทำให้เรามีค่าของทั้ง 'a' และ 'b' โดยใช้วิธีดีเทอร์มิแนนต์เพื่อค้นหาราก เราจะได้ค่า 'a' และ 'b' เป็น,
$a=(N-\sqrt{N*N-4N)}/2\\ b=(N+\sqrt{N*N-4N)}/2 $
ตัวอย่าง
#include <iostream>
//header file for the square root function
#include <math.h>
using namespace std;
int main() {
float N = 12,a,b;
cin >> N;
//using determinant method to find roots
a = (N + sqrt(N*N - 4*N))/2;
b = (N - sqrt(N*N - 4*N))/2;
cout << "The two integers are :" << endl;
cout << "a - " << a << endl;
cout << "b - " << b << endl;
return 0;
} ผลลัพธ์
The two integers are : a - 10.899 b - 1.10102