ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อค้นหาตัวเลขสองตัวที่มีผลรวมและผลิตภัณฑ์เหมือนกันกับ N
สำหรับสิ่งนี้เราจะได้รับค่าจำนวนเต็ม งานของเราคือการหาค่าจำนวนเต็มอีกสองค่าซึ่งผลคูณและผลรวมเท่ากับค่าที่กำหนด
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//finding a and b such that
//a*b=N and a+b=N
void calculateTwoValues(double N) {
double val = N * N - 4.0 * N;
if (val < 0) {
cout << "NO";
return;
}
double a = (N + sqrt(val)) / 2.0;
double b = (N - sqrt(val)) / 2.0;
cout << "Value of A:" << a << endl;
cout << "Value of B:" << b << endl;
}
int main() {
double N = 57.0;
calculateTwoValues(N);
return 0;
} ผลลัพธ์
Value of A:55.9818 Value of B:1.01819