📌  相关文章
📜  幸运的活着的人围成一圈|套装– 2

📅  最后修改于: 2021-05-25 07:37:34             🧑  作者: Mango

假设N个人(编号1到N)站成一个圆圈。他们所有人的枪都指向他们最左边的伙伴。
每一个芽使得1芽2、3芽4、5芽6…。 (N-1)芽N(如果N等于N,则N芽1)。
再次在第二次迭代中,他们按照上面提到的逻辑拍摄其余的残骸(现在,对于n来说,偶数为1,将变为3,5将变为7,依此类推)。

任务是找到最幸运的人(没有死)?

例子

这个问题已经在幸运的活着的人中讨论过了。剑谜的代码解决方案。在这篇文章中,讨论了一种不同的方法。

方法:

  1. 取N的二元等效项。
  2. 找出其1的补码并转换为其相等的十进制数N`。
  3. 找到| N – N` |。

下面是上述方法的实现:

// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to convert string to number
int stringToNum(string s)
{
  
    // object from the class stringstream
    stringstream geek(s);
  
    // The object has the value 12345 and stream
    // it to the integer x
    int x = 0;
    geek >> x;
  
    return x;
}
  
// Function to convert binary to decimal
int binaryToDecimal(string n)
{
    int num = stringToNum(n);
    int dec_value = 0;
  
    // Initializing base value to 1, i.e 2^0
    int base = 1;
  
    int temp = num;
    while (temp) {
        int last_digit = temp % 10;
        temp = temp / 10;
  
        dec_value += last_digit * base;
  
        base = base * 2;
    }
  
    return dec_value;
}
  
string itoa(int num, string str, int base)
{
    int i = 0;
    bool isNegative = false;
  
    /* Handle 0 explicitly, otherwise 
    empty string is printed for 0 */
    if (num == 0) {
        str[i++] = '0';
        return str;
    }
  
    // In standard itoa(), negative numbers
    // are handled only with base 10.
    // Otherwise numbers are considered unsigned.
    if (num < 0 && base == 10) {
        isNegative = true;
        num = -num;
    }
  
    // Process individual digits
    while (num != 0) {
        int rem = num % base;
        str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
        num = num / base;
    }
  
    // If the number is negative, append '-'
    if (isNegative)
        str[i++] = '-';
  
    // Reverse the string
    reverse(str.begin(), str.end());
  
    return str;
}
  
char flip(char c)
{
    return (c == '0') ? '1' : '0';
}
  
// Function to find the ones complement
string onesComplement(string bin)
{
    int n = bin.length(), i;
  
    string ones = "";
  
    // for ones complement flip every bit
    for (i = 0; i < n; i++)
        ones += flip(bin[i]);
  
    return ones;
}
  
// Driver code
int main()
{
    // Taking the number of a person
    // standing in a circle.
    int N = 3;
    string arr = "";
  
    // Storing the binary equivalent in a string.
    string ans(itoa(N, arr, 2));
  
    // taking one's compelement and
    // convert it to decimal value
    int N_dash = binaryToDecimal(onesComplement(ans));
  
    int luckiest_person = N - N_dash;
  
    cout << luckiest_person;
  
    return 0;
}
输出:
3


替代性的更短实施:

这里使用的方法是相同的。

// C++ implementation of the above approach
#include 
using namespace std;
  
int luckiest_person(int n)
{
    // to calculate the number of bits in
    // the binary equivalent of n
    int len = log2(n) + 1; 
  
    // Finding complement by inverting the
    // bits one by one from last
    int n2 = n;
    for (int i = 0; i < len; i++) {
  
        // XOR of n2 with (1<
输出:
3

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。