이 튜토리얼에서는 x + y =n 및 x * y =n인 두 숫자를 찾는 프로그램을 작성할 것입니다. 때때로 이러한 유형의 숫자를 찾는 것이 불가능합니다. 없음 을 인쇄합니다. 그러한 숫자가 없는 경우. 시작하겠습니다.
주어진 숫자는 이차 방정식의 합과 곱입니다. 따라서 n 2 이면 숫자가 존재하지 않습니다. - 4*n<0. 그렇지 않으면 숫자는 $$\lgroup n + \sqrt n^{2} - 4*n\rgroup/2$$ 및 $$\lgroup n - \sqrt n^{2}이 됩니다. - 4*n\r그룹/2$$.
예시
코드를 봅시다.
#include <bits/stdc++.h>
using namespace std;
void findTwoNumbersWithSameSumAndProduc(double n) {
double imaginaryValue = n * n - 4.0 * n;
// checking for imaginary roots
if (imaginaryValue < 0) {
cout << "None";
return;
}
// printing the x and y
cout << (n + sqrt(imaginaryValue)) / 2.0 << endl;
cout << (n - sqrt(imaginaryValue)) / 2.0 << endl;
}
int main() {
double n = 50;
findTwoNumbersWithSameSumAndProduc(n);
return 0;
} 출력
위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.
48.9792 1.02084
결론
튜토리얼에서 질문이 있는 경우 댓글 섹션에 언급하세요.