📌  相关文章
📜  给定范围 [L, R] 中的数字计数,它是完美的正方形,并且数字是波形

📅  最后修改于: 2022-05-13 01:56:10.481000             🧑  作者: Mango

给定范围 [L, R] 中的数字计数,它是完美的正方形,并且数字是波形

给定两个整数LR ,任务是计算[L, R]范围内的整数,使它们满足以下两个属性:

  • 该数字必须是任何整数的完全平方。
  • 整数的位数必须是波形,即令 d1 , d2 , d3 , d4 , d5为当前整数中的位数,则d1 < d2 > d3 < d4...必须成立。

例子:

朴素方法:解决问题的最简单方法是在范围 [L, R] 中遍历,并为范围内的每个数字检查上述两个条件。
时间复杂度: O(N)
辅助空间: O(1)

有效方法:为了优化上述方法,只迭代完美的正方形并检查第二个条件。请按照以下步骤操作:

  • 初始化一个变量,比如count = 0 ,以计算[L, R]范围内的所有特殊数字。
  • 迭代所有小于R的完美正方形。
  • 定义一个函数,比如check(N) ,通过遍历偶数和奇数来检查数字N是否满足第二个条件。
  • 增加count ,如果数字大于L并且函数检查对于给定的数字返回 true。
  • 最后,返回count

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Utility function to check if
// the digits of the current
// integer forms a wave pattern
bool check(int N)
{
    // Convert the number to a string
    string S = to_string(N);
 
    // Loop to iterate over digits
    for (int i = 0; i < S.size(); i++) {
        if (i == 0) {
 
            // Next character of
            // the number
            int next = i + 1;
 
            // Current character is
            // not a local minimum
            if (next < S.size()) {
                if (S[i] >= S[next]) {
 
                    return false;
                }
            }
        }
 
        else if (i == S.size() - 1) {
 
            // Previous character of
            // the number
            int prev = i - 1;
            if (prev >= 0) {
 
                // Character is a
                // local maximum
                if (i & 1) {
 
                    // Character is not
                    // a local maximum
                    if (S[i] <= S[prev]) {
                        return false;
                    }
                }
                else {
                    // Character is a
                    // local minimum
                    if (S[i] >= S[prev]) {
                        return false;
                    }
                }
            }
        }
        else {
            int prev = i - 1;
            int next = i + 1;
            if (i & 1) {
 
                // Character is a
                // local maximum
                if ((S[i] > S[prev])
                    && (S[i] > S[next])) {
                }
                else {
                    return false;
                }
            }
            else {
                // Character is a
                // local minimum
                if ((S[i] < S[prev])
                    && (S[i] < S[next])) {
                }
                else {
                    return false;
                }
            }
        }
    }
    return true;
}
 
// Function to calculate total
// integer in the given range
int totalUniqueNumber(int L, int R)
{
    // Base case
    if (R <= 0) {
        return 0;
    }
 
    // Current number
    int cur = 1;
 
    // Variable to store
    // total unique numbers
    int count = 0;
 
    // Iterating over perfect
    // squares
    while ((cur * cur) <= R) {
        int num = cur * cur;
 
        // If number is greater
        // than L and satisfies
        // required conditions
        if (num >= L && check(num)) {
            count++;
        }
 
        // Moving to the
        // next number
        cur++;
    }
 
    // Return Answer
    return count;
}
 
// Driver Code
int main()
{
    int L = 1, R = 64;
    cout << totalUniqueNumber(L, R);
}


Java
// Java  program for the above approach
import java.util.*;
public class GFG
{
     
  // Utility function to check if
  // the digits of the current
  // integer forms a wave pattern
  static boolean check(int N)
  {
     
    // Convert the number to a string
    String S = Integer.toString(N);
 
    // Loop to iterate over digits
    for (int i = 0; i < S.length(); i++) {
      if (i == 0) {
 
        // Next character of
        // the number
        int next = i + 1;
 
        // Current character is
        // not a local minimum
        if (next < S.length()) {
          if (S.charAt(i) >= S.charAt(next)) {
 
            return false;
          }
        }
      }
 
      else if (i == S.length() - 1) {
 
        // Previous character of
        // the number
        int prev = i - 1;
        if (prev >= 0) {
 
          // Character is a
          // local maximum
          if ((i & 1) == 1) {
 
            // Character is not
            // a local maximum
            if (S.charAt(i) <= S.charAt(prev)) {
              return false;
            }
          }
          else
          {
             
            // Character is a
            // local minimum
            if (S.charAt(i) >= S.charAt(prev)) {
              return false;
            }
          }
        }
      }
      else {
        int prev = i - 1;
        int next = i + 1;
        if ((i & 1) == 1) {
 
          // Character is a
          // local maximum
          if ((S.charAt(i) > S.charAt(prev))
              && (S.charAt(i) > S.charAt(next))) {
          }
          else {
            return false;
          }
        }
        else
        {
           
          // Character is a
          // local minimum
          if ((S.charAt(i) < S.charAt(prev))
              && (S.charAt(i) < S.charAt(next))) {
          }
          else {
            return false;
          }
        }
      }
    }
    return true;
  }
 
  // Function to calculate total
  // integer in the given range
  static int totalUniqueNumber(int L, int R)
  {
     
    // Base case
    if (R <= 0) {
      return 0;
    }
 
    // Current number
    int cur = 1;
 
    // Variable to store
    // total unique numbers
    int count = 0;
 
    // Iterating over perfect
    // squares
    while ((cur * cur) <= R) {
      int num = cur * cur;
 
      // If number is greater
      // than L and satisfies
      // required conditions
      if (num >= L && check(num)) {
        count++;
      }
 
      // Moving to the
      // next number
      cur++;
    }
 
    // Return Answer
    return count;
  }
 
  // Driver Code
  public static void main(String args[])
  {
      int L = 1, R = 64;
 
      // Function call
      System.out.println(totalUniqueNumber(L, R));
  }
}
// This code is contributed by Samim Hossain Mondal.


Python3
# Python  program for the above approach
 
# Utility function to check if
# the digits of the current
# integer forms a wave pattern
def check(N):
 
    # Convert the number to a string
    S = str(N);
 
    # Loop to iterate over digits
    for i in range(len(S)):
        if (i == 0):
 
            # Next character of
            # the number
            next = i + 1;
 
            # Current character is
            # not a local minimum
            if (next < len(S)):
                if (S[i] >= S[next]):
 
                    return False;
                 
        elif(i == len(S) - 1):
 
            # Previous character of
            # the number
            prev = i - 1;
            if (prev >= 0):
 
                # Character is a
                # local maximum
                if ((i & 1) == 1):
 
                    # Character is not
                    # a local maximum
                    if (S[i] <= S[prev]):
                        return False;
                else:
 
                    # Character is a
                    # local minimum
                    if (S[i] >= S[prev]):
                        return False;
                 
        else:
            prev = i - 1;
            next = i + 1;
            if ((i & 1) == 1):
 
                # Character is a
                # local maximum
                if ((S[i] > S[prev]) and (S[i] > S[next])):
                    return True;
                else:
                    return False;
                 
            else:
 
                # Character is a
                # local minimum
                if ((S[i] < S[prev]) and (S[i] < S[next])):
                    return True;
                else:
                    return False;
                 
    return True;
 
# Function to calculate total
# integer in the given range
def totalUniqueNumber(L, R):
 
    # Base case
    if (R <= 0):
        return 0;
     
    # Current number
    cur = 1;
 
    # Variable to store
    # total unique numbers
    count = 0;
 
    # Iterating over perfect
    # squares
    while ((cur * cur) <= R):
        num = cur * cur;
 
        # If number is greater
        # than L and satisfies
        # required conditions
        if (num >= L and check(num)):
            count += 1;
         
        # Moving to the
        # next number
        cur += 1;
     
    # Return Answer
    return count;
 
# Driver Code
if __name__ == '__main__':
    L = 1;
    R = 64;
 
    # Function call
    print(totalUniqueNumber(L, R));
 
# This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG
{
 
  // Utility function to check if
  // the digits of the current
  // integer forms a wave pattern
  static bool check(int N)
  {
    // Convert the number to a string
    string S = N.ToString();
 
    // Loop to iterate over digits
    for (int i = 0; i < S.Length; i++) {
      if (i == 0) {
 
        // Next character of
        // the number
        int next = i + 1;
 
        // Current character is
        // not a local minimum
        if (next < S.Length) {
          if (S[i] >= S[next]) {
 
            return false;
          }
        }
      }
 
      else if (i == S.Length - 1) {
 
        // Previous character of
        // the number
        int prev = i - 1;
        if (prev >= 0) {
 
          // Character is a
          // local maximum
          if ((i & 1) == 1) {
 
            // Character is not
            // a local maximum
            if (S[i] <= S[prev]) {
              return false;
            }
          }
          else {
            // Character is a
            // local minimum
            if (S[i] >= S[prev]) {
              return false;
            }
          }
        }
      }
      else {
        int prev = i - 1;
        int next = i + 1;
        if ((i & 1) == 1) {
 
          // Character is a
          // local maximum
          if ((S[i] > S[prev])
              && (S[i] > S[next])) {
          }
          else {
            return false;
          }
        }
        else {
          // Character is a
          // local minimum
          if ((S[i] < S[prev])
              && (S[i] < S[next])) {
          }
          else {
            return false;
          }
        }
      }
    }
    return true;
  }
 
  // Function to calculate total
  // integer in the given range
  static int totalUniqueNumber(int L, int R)
  {
    // Base case
    if (R <= 0) {
      return 0;
    }
 
    // Current number
    int cur = 1;
 
    // Variable to store
    // total unique numbers
    int count = 0;
 
    // Iterating over perfect
    // squares
    while ((cur * cur) <= R) {
      int num = cur * cur;
 
      // If number is greater
      // than L and satisfies
      // required conditions
      if (num >= L && check(num)) {
        count++;
      }
 
      // Moving to the
      // next number
      cur++;
    }
 
    // Return Answer
    return count;
  }
 
 
  // Driver Code
  public static void Main()
  {
    int L = 1, R = 64;
 
    // Function call
    Console.Write(totalUniqueNumber(L, R));
 
  }
}
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
7

时间复杂度: O(sqrt(N))
辅助空间: O(1)