考虑具有以下规则的特殊的工程师和医生家族:
- 每个人都有两个孩子。
- 工程师的第一个孩子是工程师,第二个孩子是医生。
- Doctor的第一个孩子是Doctor,第二个孩子是工程师。
- 所有世代的医生和工程师都始于工程师。
我们可以使用下图来表示这种情况:
E
/
E D
/ /
E D D E
/ / / /
E D D E D E E D
给定一个人在祖先树中的级别和位置,找到该人的职业。
例子 :
Input : level = 4, pos = 2
Output : Doctor
Input : level = 3, pos = 4
Output : Engineer
方法1(递归)
这个想法是基于一个人的职业取决于以下两个事实。
- 父母的职业。
- 节点的位置:如果节点的位置是奇数,则其职业与其父代相同。其他职业不同于其父职业。
我们递归地找到父行业,然后使用上面的第2点来找到当前节点的行业。
以下是上述想法的实现。
C++
Java
Python3
C#
PHP
Javascript
C++
Java
C#
using System;
// c# program to find profession of a person at
// given level and position.
public class GFG
{
/* Function to get no of set bits in binary
representation of passed binary no. */
public static int countSetBits(int n)
{
int count = 0;
while (n != 0)
{
n &= (n - 1);
count++;
}
return count;
}
// Returns 'e' if profession of node at given level
// and position is engineer. Else doctor. The function
// assumes that given position and level have valid values.
public static char findProffesion(int level, int pos)
{
// Count set bits in 'pos-1'
int c = countSetBits(pos - 1);
// If set bit count is odd, then doctor, else engineer
return (c % 2 != 0)? 'd' : 'e';
}
// Driver code
public static void Main(string[] args)
{
int level = 3, pos = 4;
string prof = (findProffesion(level, pos) == 'e')? "Engineer" : "Doctor";
Console.Write(prof);
}
}
// This code is contributed by Shrikant13
PHP
Javascript
输出 :
// C++ program to find profession of a person at
// given level and position.
#include
using namespace std;
// Returns 'e' if profession of node at given level
// and position is engineer. Else doctor. The function
// assumes that given position and level have valid values.
char findProffesion(int level, int pos)
{
// Base case
if (level == 1)
return 'e';
// Recursively find parent's profession. If parent
// is a Doctor, this node will be a Doctor if it is
// at odd position and an engineer if at even position
if (findProffesion(level-1, (pos+1)/2) == 'd')
return (pos%2)? 'd' : 'e';
// If parent is an engineer, then current node will be
// an engineer if at add position and doctor if even
// position.
return (pos%2)? 'e' : 'd';
}
// Driver code
int main(void)
{
int level = 4, pos = 2;
(findProffesion(level, pos) == 'e')? cout << "Engineer"
: cout << "Doctor" ;
return 0;
}
方法2(使用按位运算符)
// Java program to find
// profession of a person
// at given level and position
import java.io.*;
class GFG
{
// Returns 'e' if profession
// of node at given level
// and position is engineer.
// Else doctor. The function
// assumes that given position
// and level have valid values.
static char findProffesion(int level,
int pos)
{
// Base case
if (level == 1)
return 'e';
// Recursively find parent's
// profession. If parent
// is a Doctor, this node
// will be a Doctor if it
// is at odd position and an
// engineer if at even position
if (findProffesion(level - 1,
(pos + 1) / 2) == 'd')
return (pos % 2 > 0) ?
'd' : 'e';
// If parent is an engineer,
// then current node will be
// an engineer if at add
// position and doctor if even
// position.
return (pos % 2 > 0) ?
'e' : 'd';
}
// Driver code
public static void main (String[] args)
{
int level = 4, pos = 2;
if(findProffesion(level,
pos) == 'e')
System.out.println("Engineer");
else
System.out.println("Doctor");
}
}
// This code is contributed
// by anuj_67.
级别输入不是必需的(如果忽略最大位置限制),因为第一个元素是相同的。
结果基于位置减一的二进制表示形式中的1计数。如果计数为1则结果为工程师,否则为Doctor。
当然,头寸限制为2 ^(Level-1)
C++
# python 3 program to find profession of a person at
# given level and position.
# Returns 'e' if profession of node at given level
# and position is engineer. Else doctor. The function
# assumes that given position and level have valid values.
def findProffesion(level, pos):
# Base case
if (level == 1):
return 'e'
# Recursively find parent's profession. If parent
# is a Doctor, this node will be a Doctor if it is
# at odd position and an engineer if at even position
if (findProffesion(level-1, (pos+1)//2) == 'd'):
if (pos%2):
return 'd'
else:
return 'e'
# If parent is an engineer, then current node will be
# an engineer if at add position and doctor if even
# position.
if(pos%2):
return 'e'
else:
return 'd'
# Driver code
if __name__ == '__main__':
level = 3
pos = 4
if(findProffesion(level, pos) == 'e'):
print("Engineer")
else:
print("Doctor")
# This code is contributed by
# Surendra_Gangwar
Java
// C# program to find
// profession of a person
// at given level and position
using System;
class GFG
{
// Returns 'e' if profession
// of node at given level
// and position is engineer.
// Else doctor. The function
// assumes that given position
// and level have valid values.
static char findProffesion(int level,
int pos)
{
// Base case
if (level == 1)
return 'e';
// Recursively find parent's
// profession. If parent
// is a Doctor, this node
// will be a Doctor if it
// is at odd position and an
// engineer if at even position
if (findProffesion(level - 1,
(pos + 1) / 2) == 'd')
return (pos % 2 > 0) ?
'd' : 'e';
// If parent is an engineer,
// then current node will be
// an engineer if at add
// position and doctor if even
// position.
return (pos % 2 > 0) ?
'e' : 'd';
}
// Driver code
public static void Main ()
{
int level = 4, pos = 2;
if(findProffesion(level,
pos) == 'e')
Console.WriteLine("Engineer");
else
Console.WriteLine("Doctor");
}
}
// This code is contributed
// by anuj_67.
C#
的PHP
Java脚本
// C++ program to find profession of a person at
// given level and position.
#include
using namespace std;
/* Function to get no of set bits in binary
representation of passed binary no. */
int countSetBits(int n)
{
int count = 0;
while (n)
{
n &= (n-1) ;
count++;
}
return count;
}
// Returns 'e' if profession of node at given level
// and position is engineer. Else doctor. The function
// assumes that given position and level have valid values.
char findProffesion(int level, int pos)
{
// Count set bits in 'pos-1'
int c = countSetBits(pos-1);
// If set bit count is odd, then doctor, else engineer
return (c%2)? 'd' : 'e';
}
// Driver code
int main(void)
{
int level = 3, pos = 4;
(findProffesion(level, pos) == 'e')? cout << "Engineer"
: cout << "Doctor" ;
return 0;
}
输出 :
// Java program to find profession of a person at
// given level and position.
class GFG{
/* Function to get no of set bits in binary
representation of passed binary no. */
static int countSetBits(int n)
{
int count = 0;
while (n!=0)
{
n &= (n-1) ;
count++;
}
return count;
}
// Returns 'e' if profession of node at given level
// and position is engineer. Else doctor. The function
// assumes that given position and level have valid values.
static char findProffesion(int level, int pos)
{
// Count set bits in 'pos-1'
int c = countSetBits(pos-1);
// If set bit count is odd, then doctor, else engineer
return (c%2 !=0)? 'd' : 'e';
}
// Driver code
public static void main(String [] args)
{
int level = 3, pos = 4;
String prof = (findProffesion(level, pos) == 'e')? "Engineer"
: "Doctor" ;
System.out.print(prof);
}
}