📜  3位Osiris号码

📅  最后修改于: 2021-04-27 06:08:06             🧑  作者: Mango

给定一个三位数的数字N ,任务是查找N是否为Osiris数字。奥西里斯数是等于其自身数字的子样本的置换总和的数字。例如, 132是Osiris数,因为它等于12 + 21 + 13 + 31 + 23 + 32

例子:

方法:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if
// n is an Osiris number
bool isOsiris(int n)
{
    // 3rd digit
    int a = n % 10;
  
    // 2nd digit
    int b = (n / 10) % 10;
  
    // 1st digit
    int c = n / 100;
  
    int digit_sum = a + b + c;
  
    // Check the required condition
    if (n == (2 * (digit_sum)*11)) {
        return true;
    }
  
    return false;
}
  
// Driver code
int main()
{
    int n = 132;
    if (isOsiris(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function that returns true if
// n is an Osiris number
static boolean isOsiris(int n)
{
    // 3rd digit
    int a = n % 10;
  
    // 2nd digit
    int b = (n / 10) % 10;
  
    // 1st digit
    int c = n / 100;
  
    int digit_sum = a + b + c;
  
    // Check the required condition
    if (n == (2 * (digit_sum)*11)) 
    {
        return true;
    }
  
    return false;
}
  
// Driver code
public static void main(String args[])
{
    int n = 132;
    if (isOsiris(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Akanksha Rai


Python3
# Python implementation of the approach
  
# Function that returns true if 
# n is an Osiris number
def isOsiris(n):
      
    # 3rd digit 
    a = n % 10
      
    # 2nd digit
    b = (n//10)% 10
      
    # 1st digit
    c = n//100
  
    digit_sum = a + b + c
  
    # Check the required condition
    if(n == (2 * (digit_sum) * 11)):
        return True
      
    return False
  
# Driver code
if __name__ == '__main__':
    n = 132
    if isOsiris(n):
        print("Yes")
    else :
        print("No")


C#
// C# implementation of the approach
using System;
  
class GFG
{
// Function that returns true if
// n is an Osiris number
static bool isOsiris(int n)
{
    // 3rd digit
    int a = n % 10;
  
    // 2nd digit
    int b = (n / 10) % 10;
  
    // 1st digit
    int c = n / 100;
  
    int digit_sum = a + b + c;
  
    // Check the required condition
    if (n == (2 * (digit_sum)*11)) 
    {
        return true;
    }
  
    return false;
}
  
// Driver code
static void Main()
{
    int n = 132;
    if (isOsiris(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by mits


PHP


输出:
Yes

时间复杂度:O(1)
空间复杂度:O(1)