Home > Backend Development > C++ > body text

Using C++ programming, find the number of solutions to the equation x + y + z <= n

王林
Release: 2023-08-25 11:41:24
forward
1505 people have browsed it

使用C++编程,找到方程x + y + z <= n的解的数量

In this article, we will explain the method on how to find the solution of the equation x y z<=n. In this problem, we have an equation with four variables and the task is to find the solution of the given equation. So here is a simple example&miuns;

Input: X = 1, Y = 1, Z = 1, n = 1

Output: 4

Input: X = 1, Y = 2, Z = 3, n = 4

Output: 3
Copy after login

In this problem we can simply loop through all (x, y), (y,z) by isolating each variable and checking if it satisfies the equation , the value of (x,z).

Solution

Now we will use brute force method to find the solution to the given problem.

Brute force method

In this program, we will traverse all possible values ​​of (x,y), (y,z) and (x,z) to satisfy the equation z <= n - x - y (where z is the separated variable), where 0 <= z <= Z (the same is true for other separated variables). <= n - x - y(这里z是被分离的变量),其中0 <= z <= Z(其他被分离的变量也是一样)。

Example


#include<bits/stdc++.h>
using namespace std;
int main(){
    int X = 1, Y = 2, Z = 3, n = 4; // limits of x, y, z and given n.
    int answer = 0; // counter variable.
    for(int i = 0; i <= X; i++){
        for(int j = 0; j <= Y; j++){
            int temp = (n - i) - j; // temp = n - x - y.
            if(temp >= Z){ // if n - x - y >= z so we increment the answer.
               answer++;
            }
        }
    }
    for(int i = 0; i <= X; i++){
        for(int j = 0; j <= Z; j++){
            int temp = (n - i) - j; // temp = n - x - y.
            if(temp >= Y){ // if n - x - y >= z so we increment the answer.
               answer++;
            }
        }
    }
    for(int i = 0; i <= Z; i++){
        for(int j = 0; j <= Y; j++){
            int temp = (n - i) - j; // temp = n - x - y.
            if(temp >= X){ // if n - x - y >= z so we increment the answer.
               answer++;
            }
        }
    }
    cout << answer << "\n";
}
Copy after login

Output

17
Copy after login

Explanation of the above program

In this program, we will use a nested for loop to traverse all (x ,y), (y, z) and (x, z), and check whether the equation satisfies the condition, and if so, add the answer.

Conclusion

In this article, we solved a problem to find the number of solutions that satisfy the equation x y z<= n, with a time complexity of O(X*Y). We also learned a C program to solve this problem and the complete solution. We can write the same program in other languages ​​like C, Java, Python and others.

The above is the detailed content of Using C++ programming, find the number of solutions to the equation x + y + z <= n. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template