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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP