📌  相关文章
📜  使用给定的权重(即数的幂)来平衡秤盘

📅  最后修改于: 2021-05-04 19:51:25             🧑  作者: Mango

给定一个简单的带有两个锅的加权秤,我们得到一个权重T和其他一些权重,这些权重是特定数字a的幂,我们的目标是使用给定的权重来平衡这些锅。更正式地说,我们需要满足这个方程,
T +(a的一些幂)=(a的一些其他幂)
请记住,我们恰好被赋予与一种能力相对应的权重。
例子:

T = 11    and  a = 4
Then scale can be balanced as,        
11 + 4 + 1 = 16

我们可以看到,在这个问题中,我们的第一个目标是用a的幂表示T,所以当我们用a的底数写T时,如果表示只有1和0,那么我们知道对应于1的权重就可以使T 。 例如,

If T is 10 and a is 3 then representing 10 on 
base of 3 we get 101 i.e. using 0th power of 3 
and 2nd power of 3 (1 + 9)  we can get 10

现在,如果基本表示形式的所有数字都不是1或0,那么我们就无法平衡比例,除非在基本表示形式中某个数字是(a – 1)的情况下,因为在这种情况下,我们可以将相应的幂转移到T的那一侧并将基本表示形式中的左侧数字增加1。例如,

If T is 7 and a is 3 then representing 7 on base 
3 we get 021.
Now while looping over representation from right 
to left we get 2 (which is 3 - 1), in this case, 
we can transfer corresponding power(3^1) to T’s 
side and increase the left number by 1 i.e.,
T’s side 7 + 3 = 10    and representation 101 (9 + 1)    
which has only 1s and 0s now, so scale can be balanced.

因此,现在解决此问题的算法可以表示如下,以a的底数表示T,如果基本表示的所有数字均为1或0,则可以平衡小数位数,否则,如果任何数字为(a,则从表示的右侧循环– 1),然后将左侧数字增加1,继续执行此操作,并忽略1、0,(a – 1)和一个表示形式的个案。如果处理了完整的基本表示,则可以平衡比例,否则无法平衡。

C++
// C++ code to check whether scale can be balanced or not
#include 
using namespace std;
  
// method returns true if balancing of scale is possible
bool isBalancePossible(int T, int a)
{
    // baseForm vector will store T's representation on
    // base a in reverse order
    vector baseForm;
  
    // convert T to representation on base a
    while (T) {
        baseForm.push_back(T % a);
        T /= a;
    }
  
    // make first digit of representation as 0
    baseForm.push_back(0);
  
    // loop over base representation of T
    for (int i = 0; i < baseForm.size(); i++) {
  
        // if any digit is not 0, 1, (a - 1) or a 
        // then balancing is not possible
        if (baseForm[i] != 0 && baseForm[i] != 1 && 
            baseForm[i] != (a - 1) && baseForm[i] != a) 
            return false;        
  
        // if digit is a or (a - 1) then increase left
        // index's count/ (case, when this weight is 
        // transferred to T's side)
        if (baseForm[i] == a || baseForm[i] == (a - 1)) 
            baseForm[i + 1] += 1;        
    }
  
    // if representation is processed then balancing 
    // is possible
    return true;
}
  
// Driver code to test above methods
int main()
{
    int T = 11;
    int a = 4;
  
    bool balancePossible = isBalancePossible(T, a);
    if (balancePossible) 
        cout << "Balance is possible" << endl;
    else 
        cout << "Balance is not possible" << endl;
    return 0;
}


Java
// Java code to check whether 
// scale can be balanced or not 
import java.util.*;
  
class GFG
{
  
    // method returns true if balancing 
    // of scale is possible 
    static boolean isBalancePossible(int T, int a) 
    {
        // baseForm vector will store T's 
        // representation on base a
        // in reverse order 
        Vector baseForm = new Vector<>();
        int s = 0;
          
        // convert T to representation on base a 
        while (T > 0) 
        {
            baseForm.add(T % a);
            T /= a;
            s++;
        }
  
        // make first digit of representation as 0 
        baseForm.add(0);
  
        // loop over base representation of T 
        for (int i = 0; i < s; i++) 
        {
            // if any digit is not 0, 1, (a - 1) or a 
            // then balancing is not possible 
            if (baseForm.get(i) != 0 && 
                baseForm.get(i) != 1 && 
                baseForm.get(i) != (a - 1) && 
                baseForm.get(i) != a) 
            {
                return false;
            }
  
            // if digit is a or (a - 1) then increase left 
            // index's count/ (case, when this weight is 
            // transferred to T's side) 
            if (baseForm.get(i) == a || baseForm.get(i) == (a - 1)) 
            {
                baseForm.add(i + 1, baseForm.get(i + 1) + 1);
            }
        }
  
        // if representation is processed  
        // then balancing is possible 
        return true;
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        int T = 11;
        int a = 4;
  
        boolean balancePossible = isBalancePossible(T, a);
        if (balancePossible) 
        {
            System.out.println("Balance is possible");
        } 
        else
        {
            System.out.println("Balance is not possible");
        }
    }
}
  
// This code has been contributed by 29AjayKumar


C#
// C# code to check whether 
// scale can be balanced or not
using System;
using System.Collections.Generic; 
  
class GFG
{
  
    // method returns true if balancing 
    // of scale is possible 
    static bool isBalancePossible(int T, int a) 
    {
        // baseForm vector will store T's 
        // representation on base a
        // in reverse order 
        List baseForm = new List();
        int s = 0;
          
        // convert T to representation on base a 
        while (T > 0) 
        {
            baseForm.Add(T % a);
            T /= a;
            s++;
        }
  
        // make first digit of representation as 0 
        baseForm.Add(0);
  
        // loop over base representation of T 
        for (int i = 0; i < s; i++) 
        {
            // if any digit is not 0, 1, (a - 1) or a 
            // then balancing is not possible 
            if (baseForm[i] != 0 && 
                baseForm[i] != 1 && 
                baseForm[i] != (a - 1) && 
                baseForm[i] != a) 
            {
                return false;
            }
  
            // if digit is a or (a - 1) then increase left 
            // index's count/ (case, when this weight is 
            // transferred to T's side) 
            if (baseForm[i] == a || baseForm[i] == (a - 1)) 
            {
                baseForm.Insert(i + 1, baseForm[i+1] + 1);
            }
        }
  
        // if representation is processed 
        // then balancing is possible 
        return true;
    }
  
    // Driver code 
    public static void Main() 
    {
        int T = 11;
        int a = 4;
  
        bool balancePossible = isBalancePossible(T, a);
        if (balancePossible) 
        {
            Console.WriteLine("Balance is possible");
        } 
        else
        {
            Console.WriteLine("Balance is not possible");
        }
    }
}
  
/* This code contributed by PrinciRaj1992 */


PHP


输出:

Balance is possible