📌  相关文章
📜  检查是否可以通过旋转数字将所有数组元素转换为 pronic 数字

📅  最后修改于: 2021-09-06 06:18:16             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是检查是否可以通过多次旋转数组元素的数字来将所有数组元素转换为 pronic 数。

例子:

处理方法:按照以下步骤解决问题:

  • 遍历数组并检查每个数组元素,是否可以将其转换为 pronic 数。
  • 对于每个数组元素,应用所有可能的旋转并在每次旋转后检查生成的数字是否是 pronic 的。
  • 如果无法将任何数组元素转换为 pronic 数,则打印“False”
  • 否则,打印“True”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// function to check Pronic Number
bool isPronic(int x)
{
    for (int i = 0; i < (int)(sqrt(x)) + 1; i++)
    {
 
        // Checking Pronic Number
        // by multiplying consecutive
        // numbers
        if (x == i * (i + 1))
        {
            return true;
        }
    }
    return false;
}
 
// Function to check if any permutation
// of val is a pronic number or not
bool checkRot(int val)
{
 
    string temp = to_string(val);
    for (int i = 0; i < temp.length(); i++)
    {
        if (isPronic(stoi(temp)) == true)
        {
            return true;
        }
        temp = temp.substr(1, temp.size() - 1) + temp[0];
    }
    return false;
}
 
// Function to check if all array
// elements can be converted to
// a pronic number or not
bool check(int arr[], int N)
{
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // If current element
        // cannot be converted
        // to a pronic number
        if (checkRot(arr[i]) == false)
        {
            return false;
        }
    }
    return true;
}
 
// Driven Program
int main()
{
   
    // Given array
    int arr[] = { 321, 402, 246, 299 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // function call
    cout << (check(arr, N) ? "True" : "False");
 
    return 0;
}
 
// This code is contributed by Kingash.


Java
// Java program for the above approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // function to check Pronic Number
  static boolean isPronic(int x)
  {
 
    for (int i = 0; i < (int)(Math.sqrt(x)) + 1; i++) {
 
      // Checking Pronic Number
      // by multiplying consecutive
      // numbers
      if (x == i * (i + 1)) {
        return true;
      }
    }
 
    return false;
  }
 
  // Function to check if any permutation
  // of val is a pronic number or not
  static boolean checkRot(int val)
  {
    String temp = Integer.toString(val);
    for (int i = 0; i < temp.length(); i++)
    {
      if (isPronic(Integer.parseInt(temp)) == true) {
        return true;
      }
 
      temp = temp.substring(1) + temp.charAt(0);
    }
    return false;
  }
 
  // Function to check if all array
  // elements can be converted to
  // a pronic number or not
  static boolean check(int arr[], int N)
  {
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // If current element
      // cannot be converted
      // to a pronic number
      if (checkRot(arr[i]) == false)
      {
        return false;
      }
    }
    return true;
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Given array
    int arr[] = { 321, 402, 246, 299 };
    int N = arr.length;
 
    // Function call
    System.out.println(
      (check(arr, N) ? "True" : "False"));
  }
}
 
// This code is contributed by Kingash.


Python3
# Python implementation of
# the above approach
 
# Function to check if a number
# is a pronic number or not
def isPronic(n):
 
  for i in range(int(n**(1 / 2)) + 1):
    if i * (i + 1) == n:
      return True
 
  return False
 
# Function to check if any permutation
# of n is a pronic number or not
def checkRot(n):
 
  temp = str(n)
 
  for i in range(len(temp)):
 
    if isPronic(int(temp)):
      return True
 
    temp = temp[1:]+temp[0]
 
  return False
 
# Function to check if all array
# elements can be converted to
# a pronic number or not
def check(arr):
 
  # Traverse the array
  for i in arr:
 
    # If current element
    # cannot be converted
    # to a pronic number
    if not checkRot(i):
      return False
  return True
 
# Driver Code
arr = [ 321, 402, 246, 299 ]
print(check(arr))


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
  // function to check Pronic Number
  static bool isPronic(int x)
  {
    int val  = (int)Math.Sqrt(x);
    val += 1;
    for (int i = 0; i < val; i++)
    {
 
      // Checking Pronic Number
      // by multiplying consecutive
      // numbers
      if (x == i * (i + 1))
      {
        return true;
      }
    }
    return false;
  }
 
  // Function to check if any permutation
  // of val is a pronic number or not
  static bool checkRot(int val)
  {
 
    string temp = val.ToString();
    for (int i = 0; i < temp.Length; i++)
    {
      int a = Int32.Parse(temp);
      if (isPronic(a) == true)
      {
        return true;
      }
      temp = temp.Substring(1, temp.Length - 1) + temp[0];
    }
    return false;
  }
 
  // Function to check if all array
  // elements can be converted to
  // a pronic number or not
  static bool check(int []arr, int N)
  {
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // If current element
      // cannot be converted
      // to a pronic number
      if (checkRot(arr[i]) == false)
      {
        return false;
      }
    }
    return true;
  }
 
  // Driven Program
  public static void Main()
  {
 
    // Given array
    int []arr = { 321, 402, 246, 299 };
    int N = arr.Length;
 
    // function call
    Console.WriteLine(check(arr, N) ? "True" : "False");
  }
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
True

时间复杂度: O(N 3/2 )
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live