用 C++ 找出和與積均等於 N 的兩個數字
在本教程中,我們將討論一個程式,該程式找出和與積均等於 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP