📜  从它们的和、差、积、除和余数中找到 X 和 Y

📅  最后修改于: 2022-05-13 01:56:05.824000             🧑  作者: Mango

从它们的和、差、积、除和余数中找到 X 和 Y

给定5 个整数的arr[] ,表示 X+Y、X−Y、X*Y、X%Y 和 ⌊X/Y⌋ 对于两个非零整数 X 和 Y 的排序顺序,任务是找到X 和 Y 的值。

注意:如果不存在解决方案,则返回两个 0

例子

方法:可以根据以下数学观察解决问题

为了实现上述想法,使用回溯尝试所有可能的X+YXY值对。请按照以下步骤解决问题:

  • 将数组元素视为 A、B、C、D、E
  • 尝试所有可能的 X+Y 和 XY 值对。
    • 根据上述观察,从这两个值中找到 X 和 Y。
    • 如果 X 和 Y 的这两个值满足其他值,则返回。
    • 否则,请尝试另一对。

下面是上述方法的实现:

C++
// C++ code to implement the approach
 
#include 
#define ll long long
using namespace std;
 
// Function to check if X and Y
// are valid or not
pair isValid(ll A, ll B, ll C, ll D, ll E)
{
    // a represents 2*a for now
    ll a = A + B;
 
    // 2a/2 = a that must be integer
    if (ceil(float(a / 2)) != floor(float(a / 2))) {
        return make_pair(0, 0);
    }
    else {
 
        // Find value of a
        a = a / 2;
 
        // Find value of b
        ll b = A - a;
 
        // Edge Cases
        if (a == 0 || b == 0)
            return make_pair(0, 0);
        else if ((a + b) > pow(10, 3)
                 || (a - b) < pow(-10, 3))
            return make_pair(0, 0);
 
        // 1st Condition, C = a*b
        else if ((a * b == C) && (a / b == D)
                     && (a % b == E)
                 || (a * b == C) && (a / b == E)
                        && (a % b == D))
            return make_pair(a, b);
 
        // 2nd Condition, D = a*b
        else if ((a * b == D) && (a / b == C)
                     && (a % b == E)
                 || (a * b == D) && (a / b == E)
                        && (a % b == C))
            return make_pair(a, b);
 
        // 3rd Condition, E = a*b
        else if ((a * b == E) && (a / b == C)
                     && (a % b == D)
                 || (a * b == E) && (a / b == D)
                        && (a % b == C))
            return make_pair(a, b);
 
        // Pairs are not valid then return 0
        else
            return make_pair(0, 0);
    }
}
 
// Function to find two integers X and Y
 
void findNum(ll* arr)
{
    pair p;
    bool flag = 0;
 
    for (int i = 0; i <= 4; i++) {
 
        // Swaping for every
        // X + Y combination
        swap(arr[0], arr[i]);
        for (int j = 1; j <= 4; j++) {
 
            // Swaping for every
            // X - Y combination
            swap(arr[1], arr[j]);
 
            // Checking for valid X and Y
            p = isValid(arr[0], arr[1],
                        arr[2], arr[3],
                        arr[4]);
 
            // If both are not -1 then
            // we found X and Y
            if ((p.first != 0)
                && (p.second != 0)) {
 
                // Set Flag = true
                flag = 1;
 
                // Print the values in order
                // i.e., X and Y
                cout << p.first << " "
                     << p.second << endl;
            }
 
            // Backtracking
            swap(arr[1], arr[j]);
 
            // X and Y are found
            if (flag)
                break;
        }
 
        // Backtracking
        swap(arr[0], arr[i]);
 
        // X and Y are found
        if (flag)
            break;
    }
 
    // If flag is 0 then X and Y
    // can't be possible
    if (!flag)
        cout << 0 << " " << 0 << endl;
}
 
// Driver Code
int main()
{
    int N = 5;
    ll arr[N] = { -1, 0, 4, 9, 20 };
 
    // Function call
    findNum(arr);
    return 0;
}


Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to check if X and Y
  // are valid or not
  public static long[] isValid(long A, long B, long C,
                               long D, long E)
  {
    // a represents 2*a for now
    long a = A + B;
 
    // 2a/2 = a that must be integer
    if (Math.ceil((a / 2.0)) != Math.floor((a / 2.0))) {
 
      long ans[] = { 0, 0 };
      return ans;
    }
    else {
 
      // Find value of a
      a = a / 2;
 
      // Find value of b
      long b = A - a;
      long res[] = { 0, 0 };
      long res1[] = { a, b };
      // Edge Cases
      if (a == 0 || b == 0)
        return res;
      else if ((a + b) > Math.pow(10, 3)
               || (a - b) < Math.pow(-10, 3))
        return res;
 
      // 1st Condition, C = a*b
      else if ((a * b == C) && (a / b == D)
               && (a % b == E)
               || (a * b == C) && (a / b == E)
               && (a % b == D))
        return res1;
 
      // 2nd Condition, D = a*b
      else if ((a * b == D) && (a / b == C)
               && (a % b == E)
               || (a * b == D) && (a / b == E)
               && (a % b == C))
        return res1;
 
      // 3rd Condition, E = a*b
      else if ((a * b == E) && (a / b == C)
               && (a % b == D)
               || (a * b == E) && (a / b == D)
               && (a % b == C))
        return res1;
 
      // Pairs are not valid then return 0
      else
        return res;
    }
  }
 
  // Function to find two integers X and Y
  public static void findNum(long arr[])
  {
    long p[] = new long[2];
    int flag = 0;
 
    for (int i = 0; i <= 4; i++) {
 
      // Swaping for every
      // X + Y combination
      long tmp = arr[0];
      arr[0] = arr[i];
      arr[i] = tmp;
      for (int j = 1; j <= 4; j++) {
 
        // Swaping for every
        // X - Y combination
        tmp = arr[1];
        arr[1] = arr[j];
        arr[j] = tmp;
 
 
        // Checking for valid X and Y
        p = isValid(arr[0], arr[1], arr[2], arr[3],
                    arr[4]);
 
        // If both are not -1 then
        // we found X and Y
        if ((p[0] != 0) && (p[1] != 0)) {
 
          // Set Flag = true
          flag = 1;
 
          // Print the values in order
          // i.e., X and Y
          System.out.println(p[0] + " " + p[1]);
        }
 
        // Backtracking
        tmp = arr[1];
        arr[1] = arr[j];
        arr[j] = tmp;
 
        // X and Y are found
        if (flag != 0)
          break;
      }
 
      // Backtracking
      tmp = arr[0];
      arr[0] = arr[i];
      arr[i] = tmp;
 
      // X and Y are found
      if (flag != 0)
        break;
    }
 
    // If flag is 0 then X and Y
    // can't be possible
    if (flag == 0)
      System.out.println("0 0");
  }
  public static void main(String[] args)
  {
    int N = 5;
    long arr[] = { -1, 0, 4, 9, 20 };
 
    // Function call
    findNum(arr);
  }
}
 
// This code is contributed by Rohit Pradhan


C#
// C# code to implement the approach
using System;
 
public class GFG{
 
  // Function to check if X and Y
  // are valid or not
  static long[] isValid(long A, long B, long C,
                               long D, long E)
  {
    // a represents 2*a for now
    long a = A + B;
 
    // 2a/2 = a that must be integer
    if (Math.Ceiling((a / 2.0)) != Math.Floor((a / 2.0))) {
 
      long[] ans = { 0, 0 };
      return ans;
    }
    else {
 
      // Find value of a
      a = a / 2;
 
      // Find value of b
      long b = A - a;
      long[] res = { 0, 0 };
      long[] res1 = { a, b };
      // Edge Cases
      if (a == 0 || b == 0)
        return res;
      else if ((a + b) > Math.Pow(10, 3)
               || (a - b) < Math.Pow(-10, 3))
        return res;
 
      // 1st Condition, C = a*b
      else if ((a * b == C) && (a / b == D)
               && (a % b == E)
               || (a * b == C) && (a / b == E)
               && (a % b == D))
        return res1;
 
      // 2nd Condition, D = a*b
      else if ((a * b == D) && (a / b == C)
               && (a % b == E)
               || (a * b == D) && (a / b == E)
               && (a % b == C))
        return res1;
 
      // 3rd Condition, E = a*b
      else if ((a * b == E) && (a / b == C)
               && (a % b == D)
               || (a * b == E) && (a / b == D)
               && (a % b == C))
        return res1;
 
      // Pairs are not valid then return 0
      else
        return res;
    }
  }
 
  // Function to find two integers X and Y
  static void findNum(long[] arr)
  {
    long[] p = new long[2];
    int flag = 0;
 
    for (int i = 0; i <= 4; i++) {
 
      // Swaping for every
      // X + Y combination
      long tmp = arr[0];
      arr[0] = arr[i];
      arr[i] = tmp;
      for (int j = 1; j <= 4; j++) {
 
        // Swaping for every
        // X - Y combination
        tmp = arr[1];
        arr[1] = arr[j];
        arr[j] = tmp;
 
 
        // Checking for valid X and Y
        p = isValid(arr[0], arr[1], arr[2], arr[3],
                    arr[4]);
 
        // If both are not -1 then
        // we found X and Y
        if ((p[0] != 0) && (p[1] != 0)) {
 
          // Set Flag = true
          flag = 1;
 
          // Print the values in order
          // i.e., X and Y
          Console.WriteLine(p[0] + " " + p[1]);
        }
 
        // Backtracking
        tmp = arr[1];
        arr[1] = arr[j];
        arr[j] = tmp;
 
        // X and Y are found
        if (flag != 0)
          break;
      }
 
      // Backtracking
      tmp = arr[0];
      arr[0] = arr[i];
      arr[i] = tmp;
 
      // X and Y are found
      if (flag != 0)
        break;
    }
 
    // If flag is 0 then X and Y
    // can't be possible
    if (flag == 0)
      Console.WriteLine("0 0");
  }
    static public void Main (){
 
    long[] arr = { -1, 0, 4, 9, 20 };
 
    // Function call
    findNum(arr);
    }
}
 
// This code is contributed by hrithikgarg03188.


输出
4 5

时间复杂度:O(1)
辅助空间:O(1)