📜  使用化合价找出一个分子是否可以由3个原子形成

📅  最后修改于: 2021-05-08 16:36:04             🧑  作者: Mango

原子的价数定义为该原子必须与其他原子形成的键的确切数目。给定3个原子的化合价,任务是确定它们是否可以一起形成一个分子。原子可以彼此形成多个键。

例子:

Input: 2 4 2
Output: YES
The bonds are between the following atoms:
1 - 2
1 - 2
2 - 3
2 - 3

Input: 1 2 3
Output: NO

方法:让化合价为a,b和c。令c为最大。我们有2种无法形成分子的情况:

  • a + b + c是奇数:由于每个键将2个原子的化合价减少1,所以化合价应为偶数。
  • a + b 在这种情况下,即使每个键都形成了c,也将使c不满足要求。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to check if it is possible
void printPossible(int a, int b, int c)
{
    if ((a + b + c) % 2 != 0 || a + b < c)
        cout << "NO";
    else
        cout << "YES";
}
  
// Driver code
int main()
{
    int a = 2, b = 4, c = 2;
    printPossible(a, b, c);
  
  return 0;
}


Java
// Java implementation of the above approach 
  
import java.io.*;
  
class GFG {
    // Function to check if it is possible 
static void printPossible(int a, int b, int c) 
{ 
    if ((a + b + c) % 2 != 0 || a + b < c) 
        System.out.println("NO"); 
    else
        System.out.println("YES"); 
} 
  
// Driver code 
    public static void main (String[] args) {
  
    int a = 2, b = 4, c = 2; 
    printPossible(a, b, c); 
    } 
} 
  
// This code is contributed by akt_mit


Python3
# Python 3 implementation of the
# above approach 
  
# Function to check if it is possible 
def printPossible( a, b, c):
  
    if ((a + b + c) % 2 != 0 or a + b < c):
        print ("NO")
    else:
        print ("YES")
  
# Driver code 
if __name__ == "__main__":
  
    a = 2
    b = 4
    c = 2
    printPossible(a, b, c)
  
# This code is contributed 
# by ChitraNayal


C#
// C# implementation of the above approach
using System;
  
class GFG
{
  
// Function to check if it is possible
static void printPossible(int a, int b, int c)
{
    if ((a + b + c) % 2 != 0 || a + b < c)
        Console.Write("NO");
    else
        Console.Write("YES");
}
  
// Driver code
public static void Main()
{
    int a = 2, b = 4, c = 2;
    printPossible(a, b, c);
}
}
  
// This code is contributed
// by Akanksha Rai


PHP


输出:

Yes

时间复杂度: O(1)