C ++程序查找两个数之和与N都相同的乘积
在本教程中,我们将讨论一个程序来查找两个数字(例如“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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短