从 N 个球中找到唯一更重的球所需的最小比较
给定N (N > 1)个球和一个重量平衡机。有N-1个相同重量的球,一个球比其他球重。任务是找出每次可以对任意数量的球称重的较重的球所需的最少称重次数。
注意:重量平衡机可以分辨相对重量而不是绝对重量,即一个球相对于另一个球的重量。
例子:
Input: N = 5
Output: 2
Explanation: Divide the balls into 2+2+1 parts. Weigh the groups with 2 balls.
If they weigh the same the other is having a different weight.
Otherwise, take the group with greater weight(as it has the required ball) and
measure them one vs one(2 times measuring case).
Input: N = 9
Output: 2
天真的方法:该解决方案基于以下观察。
- 任意数量的球(N)可以分成3 组,球的数量几乎相等,并且一次可以对其中的两个进行加权。
- 现在,如果其中任何一个比另一个重,则较重的球在该组中。
- 否则,它在未加权的组中。
- 现在,该组又可以进一步分为 3 个子组,然后继续。
- 所以这给出了一个递归函数。
因此,请使用递归函数,其中 N 在每一步中除以 3,并且现在在 N/3 的上限值上调用递归。
下面是上述方法的实现。
C++
// C++ code to implement above approach
#include
using namespace std;
// Recursive function to find
// minimum number of weighing required
int solve(int N)
{
if (N == 0)
return 0;
if (N == 1)
return 0;
float rec = N;
return (solve(ceil(rec / 3.0)) + 1);
}
// Driver code
int main()
{
int N = 5;
cout << solve(N);
return 0;
}
Java
// Java code to implement above approach
class GFG
{
// Recursive function to find
// minimum number of weighing required
public static int solve(int N) {
if (N == 0)
return 0;
if (N == 1)
return 0;
float rec = N;
return (solve((int)Math.ceil(rec / 3.0)) + 1);
}
// Driver code
public static void main(String args[]) {
int N = 5;
System.out.println(solve(N));
}
}
// This code is contributed by gfgking.
Python3
# Python code to implement above approach
import math;
# Recursive function to find
# minimum number of weighing required
def solve(N):
if (N == 0):
return 0;
if (N == 1):
return 0;
rec = N;
return (solve(math.ceil(rec / 3.0)) + 1);
# Driver code
N = 5;
print(solve(N));
# This code is contributed by gfgking.
Javascript
C#
using System;
public class GFG {
// Recursive function to find
// minimum number of weighing required
public static int solve(int N)
{
if (N == 0)
return 0;
if (N == 1)
return 0;
float rec = N;
return (solve((int)Math.Ceiling(rec / 3.0)) + 1);
}
// Driver code
static public void Main()
{
// Code
int N = 5;
Console.Write(solve(N));
}
}
// This code is contributed by Potta lokesh
C++
// C++ code to implement above approach
#include
using namespace std;
// Functions to find the minimum
// number of weighing required
int solve(int N)
{
int mini = ceil(log(N) / log(3));
return mini;
}
// Driver code
int main()
{
int N = 5;
cout << solve(N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Functions to find the minimum
// number of weighing required
static int solve(int N)
{
int mini = (int)Math.ceil(Math.log(N) / Math.log(3));
return mini;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
System.out.print(solve(N));
}
}
// This code is contributed by code_hunt.
Python
# Python code to implement above approach
import math
# Functions to find the minimum
# number of weighing required
def solve(N):
mini = math.ceil(math.log(N) / math.log(3))
return mini
# Driver code
N = 5
print(solve(N))
# This code is contributed by Samim Hossain Mondal.
C#
// C# code for the above approach
using System;
public class GFG{
// Functions to find the minimum
// number of weighing required
static int solve(int N)
{
int mini = (int)Math.Ceiling(Math.Log(N) / Math.Log(3));
return mini;
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
Console.Write(solve(N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
2
时间复杂度: O(log(N))
辅助空间: O(1)
有效方法:从上面的方法可以清楚地看出,它类似于找到等于或大于 N的 3 的指数。可以使用对数计算。
C++
// C++ code to implement above approach
#include
using namespace std;
// Functions to find the minimum
// number of weighing required
int solve(int N)
{
int mini = ceil(log(N) / log(3));
return mini;
}
// Driver code
int main()
{
int N = 5;
cout << solve(N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Functions to find the minimum
// number of weighing required
static int solve(int N)
{
int mini = (int)Math.ceil(Math.log(N) / Math.log(3));
return mini;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
System.out.print(solve(N));
}
}
// This code is contributed by code_hunt.
Python
# Python code to implement above approach
import math
# Functions to find the minimum
# number of weighing required
def solve(N):
mini = math.ceil(math.log(N) / math.log(3))
return mini
# Driver code
N = 5
print(solve(N))
# This code is contributed by Samim Hossain Mondal.
C#
// C# code for the above approach
using System;
public class GFG{
// Functions to find the minimum
// number of weighing required
static int solve(int N)
{
int mini = (int)Math.Ceiling(Math.Log(N) / Math.Log(3));
return mini;
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
Console.Write(solve(N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
2
时间复杂度: O(1)
辅助空间: O(1)