📌  相关文章
📜  用给定的不等式方程找到’N’个解

📅  最后修改于: 2021-04-27 19:07:41             🧑  作者: Mango

找到a 1 ,a 2 ,a 3 ,….a n的值,使以下两个条件都满足。
a_1^2 + a_2^2 + a_3^2 + ....+ a_n^2 \geq X
a_1 + a_2 + a_3 + ....+ a_n \leq Y
打印a 1 ,a 2 ,…, n的值,否则打印“ No solution”。
注意:可能有几种解决方案,请打印其中的任何一种。
例子:

Input: n = 5, x = 15, y = 15
Output:
11
1
1
1
1
Input: n = 4, x = 324, y = 77
Output: 
74
1
1
1

方法:下面是解决此问题的分步算法:

  1. 初始化元素数量以及x和y的值。
  2. 如果y小于n或x非常大于n,则没有1 …a 2的解。
  3. 将第一个解决方案打印为y – n + 1,将其他元素的解决方案打印为1。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
#define ll long long
 
// Function to calculate all the solutions
void findsolution(ll n, ll x, ll y)
{
    // there is no solutions
    if ((y - n + 1) * (y - n + 1) + n - 1 < x || y < n) {
        cout << "No solution";
        return;
    }
 
    // print first element as y-n+1
    cout << y - n + 1;
 
    // print rest n-1 elements as 1
    while (n-- > 1)
        cout << endl
             << 1;
}
 
// Driver code
int main()
{
    // initialize the number of elements
    // and the value of x an y
    ll n, x, y;
    n = 5, x = 15, y = 15;
 
    findsolution(n, x, y);
 
    return 0;
}


Java
// java implementation of above approach
import java.io.*;
 
class GFG {
    
// Function to calculate all the solutions
static void findsolution(long n, long x, long y)
{
    // there is no solutions
    if ((y - n + 1) * (y - n + 1) + n - 1 < x || y < n) {
        System.out.println( "No solution");
        return;
    }
 
    // print first element as y-n+1
    System.out.println( y - n + 1);
 
    // print rest n-1 elements as 1
    while (n-- > 1)
            System.out.println( "1");
}
 
// Driver code
 
    public static void main (String[] args) {
            // initialize the number of elements
    // and the value of x an y
    long n, x, y;
    n = 5; x = 15; y = 15;
 
    findsolution(n, x, y);
    }
}
// This code is contributed
// by ajit


Python3
# Python3 implementation of above approach
 
# Function to calculate all the solutions
def findsolution(n, x, y):
 
    # there is no solutions
    if ((y - n + 1) * (y - n + 1) +
              n - 1 < x or y < n):
        print("No solution");
        return;
 
    # print first element as y-n+1
    print(y - n + 1);
 
    # print rest n-1 elements as 1
    while (n > 1):
        print(1);
        n -= 1;
 
# Driver code
 
# initialize the number of elements
# and the value of x an y
n = 5;
x = 15;
y = 15;
 
findsolution(n, x, y);
 
# This code is contributed by mits


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to calculate all the solutions
static void findsolution(long n,
                         long x, long y)
{
    // there is no solutions
    if ((y - n + 1) * (y - n + 1) +
         n - 1 < x || y < n)
    {
        Console.WriteLine( "No solution");
        return;
    }
 
    // print first element as y-n+1
    Console.WriteLine( y - n + 1);
 
    // print rest n-1 elements as 1
    while (n-- > 1)
        Console.WriteLine( "1");
}
 
// Driver code
static public void Main ()
{
    // initialize the number of elements
    // and the value of x an y
    long n, x, y;
    n = 5; x = 15; y = 15;
     
    findsolution(n, x, y);
}
}
 
// This code is contributed
// by ajit


PHP
 1)
    echo "\n" . 1;
}
 
// Driver code
 
// initialize the number of elements
// and the value of x an y
$n = 5; $x = 15; $y = 15;
 
findsolution($n, $x, $y);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Javascript


输出:
11
1
1
1
1