📜  检查是否可以使用秤和一些砝码测量物品

📅  最后修改于: 2021-04-24 22:09:30             🧑  作者: Mango

给定一些质量权重a 0 ,a 1 ,a 2 ,…, 100 ,a为整数和一个称重秤,可以在秤的两边放置砝码。检查是否可以使用这些重量和秤来测量重量W的特定项目。

例子:

方法:

  • 首先,可以仔细观察到,对于a = 2或a = 3,答案始终存在。
  • 保持包含质量权重的数组,并且仅包括小于10 9的权重
  • 现在,可以通过将当前权重包括到包含
    物品或包括当前重量到包含物品的另一侧或根本不使用该重量。

    下面是上述方法的实现。

    C++
    // CPP Program to check whether an item
    // can be measured using some weight and
    // a weighing scale.
    #include 
    using namespace std;
      
    // Variable to denote that answer has
    // been found
    int found = 0;
      
    void solve(int idx, int itemWt, int wts[],
               int N)
    {
        if (found)
            return;
      
        // Item has been measured
        if (itemWt == 0) {
            found = 1;
            return;
        }
      
        // If the index of current weight
        // is greater than totalWts
        if (idx > N)
            return;
      
        // Current weight is not included
        // on either side
        solve(idx + 1, itemWt, wts, N);
      
        // Current weight is included on the
        // side containing item
        solve(idx + 1, itemWt + wts[idx], wts,
              N);
      
        // Current weight is included on the
        // side opposite to the side
        // containing item
        solve(idx + 1, itemWt - wts[idx], wts,
              N);
    }
      
    // This function computes the required array
    // of weights using a
    bool checkItem(int a, int W)
    {
        // If the a is 2 or 3, answer always
        // exists
        if (a == 2 || a == 3)
            return 1;
      
        int wts[100]; // weights array
        int totalWts = 0; // feasible weights
        wts[0] = 1;
        for (int i = 1;; i++) {
            wts[i] = wts[i - 1] * a;
            totalWts++;
      
            // if the current weight
            // becomes greater than 1e9
            // break from the loop
            if (wts[i] > 1e9)
                break;
        }
        solve(0, W, wts, totalWts);
        if (found)
            return 1;
      
        // Item can't be measured
        return 0;
    }
      
    // Driver Code to test above functions
    int main()
    {
        int a = 2, W = 5;
        if (checkItem(a, W))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
      
        a = 4, W = 11, found = 0;
        if (checkItem(a, W))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
      
        a = 4, W = 7, found = 0;
        if (checkItem(a, W))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
        return 0;
    }


    Java
    // Java Program to check whether an item
    // can be measured using some weight and
    // a weighing scale.
      
    class GFG {
      
    // Variable to denote that answer has
    // been found
        static int found = 0;
      
        static void solve(int idx, int itemWt, int wts[], int N) {
            if (found == 1) {
                return;
            }
      
            // Item has been measured
            if (itemWt == 0) {
                found = 1;
                return;
            }
      
            // If the index of current weight
            // is greater than totalWts
            if (idx > N) {
                return;
            }
      
            // Current weight is not included
            // on either side
            solve(idx + 1, itemWt, wts, N);
      
            // Current weight is included on the
            // side containing item
            solve(idx + 1, itemWt + wts[idx], wts,
                    N);
      
            // Current weight is included on the
            // side opposite to the side
            // containing item
            solve(idx + 1, itemWt - wts[idx], wts,
                    N);
        }
      
    // This function computes the required array
    // of weights using a
        static boolean checkItem(int a, int W) {
            // If the a is 2 or 3, answer always
            // exists
            if (a == 2 || a == 3) {
                return true;
            }
      
            int wts[] = new int[100]; // weights array
            int totalWts = 0; // feasible weights
            wts[0] = 1;
            for (int i = 1;; i++) {
                wts[i] = wts[i - 1] * a;
                totalWts++;
      
                // if the current weight
                // becomes greater than 1e9
                // break from the loop
                if (wts[i] > 1e9) {
                    break;
                }
            }
            solve(0, W, wts, totalWts);
            if (found == 1) {
                return true;
            }
      
            // Item can't be measured
            return false;
        }
      
    // Driver Code to test above functions
        public static void main(String[] args) {
      
            int a = 2, W = 5;
            if (checkItem(a, W)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
      
            a = 4; W = 11;found = 0;
            if (checkItem(a, W)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
      
            a = 4; W = 7; found = 0;
            if (checkItem(a, W)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
      
    //this code contributed by Rajput-Ji


    C#
    // C# Program to check whether an item
    // can be measured using some weight and
    // a weighing scale.
    using System; 
    public class GFG {
       
    // Variable to denote that answer has
    // been found
        static int found = 0;
       
        static void solve(int idx, int itemWt, int []wts, int N) {
            if (found == 1) {
                return;
            }
       
            // Item has been measured
            if (itemWt == 0) {
                found = 1;
                return;
            }
       
            // If the index of current weight
            // is greater than totalWts
            if (idx > N) {
                return;
            }
       
            // Current weight is not included
            // on either side
            solve(idx + 1, itemWt, wts, N);
       
            // Current weight is included on the
            // side containing item
            solve(idx + 1, itemWt + wts[idx], wts,
                    N);
       
            // Current weight is included on the
            // side opposite to the side
            // containing item
            solve(idx + 1, itemWt - wts[idx], wts,
                    N);
        }
       
    // This function computes the required array
    // of weights using a
        static bool checkItem(int a, int W) {
            // If the a is 2 or 3, answer always
            // exists
            if (a == 2 || a == 3) {
                return true;
            }
       
            int []wts = new int[100]; // weights array
            int totalWts = 0; // feasible weights
            wts[0] = 1;
            for (int i = 1;; i++) {
                wts[i] = wts[i - 1] * a;
                totalWts++;
       
                // if the current weight
                // becomes greater than 1e9
                // break from the loop
                if (wts[i] > 1e9) {
                    break;
                }
            }
            solve(0, W, wts, totalWts);
            if (found == 1) {
                return true;
            }
       
            // Item can't be measured
            return false;
        }
       
    // Driver Code to test above functions
        public static void Main() {
       
            int a = 2, W = 5;
            if (checkItem(a, W)) {
                Console.WriteLine("YES");
            } else {
                Console.WriteLine("NO");
            }
       
            a = 4; W = 11;found = 0;
            if (checkItem(a, W)) {
                Console.WriteLine("YES");
            } else {
                Console.WriteLine("NO");
            }
       
            a = 4; W = 7; found = 0;
            if (checkItem(a, W)) {
                Console.WriteLine("YES");
            } else {
                Console.WriteLine("NO");
            }
        }
    }
       
    //this code contributed by Rajput-Ji


    输出:
    YES
    YES
    NO
    

    时间复杂度: O(3 N ),其中N不能大于20,因为4 20大于10 9

    想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”