📜  找到可用于称量[1,X]范围内所有重量的最佳重量

📅  最后修改于: 2021-05-04 14:58:55             🧑  作者: Mango

给定一个整数X ,任务是找到一组最佳的权重{w 1 ,w 2 ,w 3 ,…,w n },这样我们就可以使用双面加权来加权/确定从1X的所有权重平衡盘。请注意,所有权重必须唯一,并且n应该尽可能小。

例子:

方法:

  1. 一种最佳方法是使用权重为3的幂,即{1、3、9、27、81、243,…}
  2. 通过归纳可以证明,使用{1,3,9,27,81,…,pow(3,n)} ,我们可以平衡从1(pow(3,n + 1)– 1)的所有权重。 / 2
  3. 因此,找到n的值,以便可以平衡从1X的所有值并打印结果。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the optimal weights
void findWeights(int X)
{
    int sum = 0;
  
    // Number of weights required
    int power = 0;
  
    // Finding the value of required powers of 3
    while (sum < X) {
        sum = pow(3, power + 1) - 1;
        sum /= 2;
        power++;
    }
  
    // Optimal Weights are powers of 3
    int ans = 1;
    for (int i = 1; i <= power; i++) {
        cout << ans << " ";
        ans = ans * 3;
    }
}
  
// Driver code
int main()
{
    int X = 2;
  
    findWeights(X);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*; 
  
public class GFG 
{ 
      
    // Function to find the optimal weights 
    static void findWeights(int X) 
    { 
        int sum = 0; 
  
        // Number of weights required 
        int power = 0; 
        int number = 3;
          
        // Finding the value of required powers of 3 
        while (sum < X)
        { 
            sum = number - 1; 
            sum /= 2; 
            power++;
            number *= 3;
        } 
  
        // Optimal Weights are powers of 3 
        int ans = 1; 
        for (int i = 1; i <= power; i++)
        { 
            System.out.print(ans + " "); 
            ans = ans * 3; 
        } 
    } 
  
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int X = 2; 
  
        findWeights(X); 
  
    } 
} 
  
// This code is contributed by Sam007.


Python3
# Python3 implementation of the approach
  
# Function to find the optimal weights
def findWeights(X):
  
    sum = 0
  
    # Number of weights required
    power = 0
  
    # Finding the value of required powers of 3
    while (sum < X):
        sum = pow(3, power + 1) - 1
        sum //= 2
        power += 1
  
    # Optimal Weights are powers of 3
    ans = 1
    for i in range(1, power + 1):
        print(ans, end = " ")
        ans = ans * 3
  
# Driver code
X = 2
  
findWeights(X)
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
    // Function to find the optimal weights 
    static void findWeights(int X) 
    { 
        int sum = 0; 
  
        // Number of weights required 
        int power = 0; 
        int number = 3;
          
        // Finding the value of required powers of 3 
        while (sum < X)
        { 
            sum = number - 1; 
            sum /= 2; 
            power++;
            number *= 3;
        } 
  
        // Optimal Weights are powers of 3 
        int ans = 1; 
        for (int i = 1; i <= power; i++)
        { 
            Console.Write(ans + " "); 
            ans = ans * 3; 
        } 
    } 
  
    // Driver code 
    static public void Main ()
    {
        int X = 2; 
        findWeights(X); 
    } 
} 
  
// This code is contributed by ajit.


输出:
1 3