📜  检查数字的位数是否交替增加和减少

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

检查数字的位数是否交替增加和减少

给定一个数字N ,任务是检查数字的位数是否交替增加减少。如果位数小于3 ,则返回false 。例如,如果 d1、d2 和 d3 是 N 的数字,则d1 < d2 > d3必须成立
例子

方法:这个任务可以通过将给定的整数转换成字符串来解决。遍历字符串并检查相邻字符是否符合给定条件。
下面是上述方法的实现:

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;
}
 
// Driver Code
int main()
{
    int N = 64;
    cout << (check(N) ? "true" : "false");
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
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 % 2 == 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 % 2 == 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;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 64;
    if(check(N) == true)
      System.out.print("true");
    else
      System.out.print("false");
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# Python implementation 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):
 
                    # 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):
 
                # Character is a
                # local maximum
                if ((S[i] > S[prev]) and (S[i] > S[next])):
                    print("", end="")
                else:
                    return False
            else:
                # Character is a
                # local minimum
                if ((S[i] < S[prev]) and (S[i] < S[next])):
                    print("", end="")
                else:
                    return False
    return True
 
 
# Driver Code
 
N = 64
print("true" if check(N) else "false")
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
 
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 % 2 == 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 % 2 == 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;
  }
 
  // Driver Code
  public static void Main () {
    int N = 64;
    if(check(N) == true)
      Console.Write("true");
    else
      Console.Write("false");
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
false

时间复杂度:O(N),N是位数
辅助空间:O(N)