给定数字N。任务是检查数字是否遵循以下任何顺序:
- 这些数字严格按升序排列。
- 或者,数字严格按降序排列。
- 或者,数字首先严格按照升序排列,然后严格按照降序排列。
如果数字遵循上述任何顺序,则打印“是”,否则打印“否”。
例子:
Input : N = 415
Output : NO
Input : N = 123454321
Output : YES
通过从一位到一位提取每个数字,从右到左遍历数字。保留一个指针以指示当前序列是降序还是升序,-1表示严格升序,1表示严格降序。首先,随着我们从右到左的顺序,顺序应该严格增加。当我们遇到一个比前一个数字小的数字时,将标志更改为减少(即-1),然后以递增顺序获得等于前一个数字的任何数字,我们直接打印NO。
下面是上述方法的实现:
C++
// CPP program to check if the digits are in
// the given order
#include
using namespace std;
// Check if the digits follow the correct order
bool isCorrectOrder(int n)
{
bool flag = true;
// to store the previous digit
int prev = -1;
// pointer to tell what type of sequence
// are we dealing with
int type = -1;
while(n != 0)
{
if(type ==-1)
{
if(prev ==-1)
{
prev = n % 10;
n = n/10;
continue;
}
// check if we have same digit
// as the previous digit
if(prev == n % 10)
{
flag = false;
break;
}
// checking the peak point of the number
if(prev > n % 10)
{
type = 1;
prev = n % 10;
n = n/10;
continue;
}
prev = n % 10;
n = n / 10;
}
else
{
// check if we have same digit
// as the previous digit
if(prev == n % 10)
{
flag = false;
break;
}
// check if the digit is greater than
// the previous one
// If true, then break from the loop as
// we are in descending order part
if(prev < n % 10)
{
flag = false;
break;
}
prev = n % 10;
n = n / 10;
}
}
return flag;
}
// Driver code
int main()
{
int n = 123454321;
if(isCorrectOrder(n))
cout<<"YES";
else
cout<<"NO";
return 0;
}
Java
// Java program to check if the digits are in
// the given order
import java.io.*;
class GFG {
// Check if the digits follow the correct order
static boolean isCorrectOrder(int n)
{
boolean flag = true;
// to store the previous digit
int prev = -1;
// pointer to tell what type of sequence
// are we dealing with
int type = -1;
while(n != 0)
{
if(type ==-1)
{
if(prev ==-1)
{
prev = n % 10;
n = n/10;
continue;
}
// check if we have same digit
// as the previous digit
if(prev == n % 10)
{
flag = false;
break;
}
// checking the peak point of the number
if(prev > n % 10)
{
type = 1;
prev = n % 10;
n = n/10;
continue;
}
prev = n % 10;
n = n / 10;
}
else
{
// check if we have same digit
// as the previous digit
if(prev == n % 10)
{
flag = false;
break;
}
// check if the digit is greater than
// the previous one
// If true, then break from the loop as
// we are in descending order part
if(prev < n % 10)
{
flag = false;
break;
}
prev = n % 10;
n = n / 10;
}
}
return flag;
}
// Driver code
public static void main (String[] args) {
int n = 123454321;
if(isCorrectOrder(n))
System.out.println("YES");
else
System.out.println("NO");
}
}
// This code is contributed by ajit
Python3
# Python3 program to check if the
# digits are in the given order
# Check if the digits follow
# the correct order
def isCorrectOrder(n):
flag = True;
# to store the previous digit
prev = -1;
# pointer to tell what type of
# sequence are we dealing with
type = -1;
while(n != 0):
if(type ==-1):
if(prev ==-1):
prev = n % 10;
n = int(n / 10);
continue;
# check if we have same digit
# as the previous digit
if(prev == n % 10):
flag = False;
break;
# checking the peak point
# of the number
if(prev > n % 10):
type = 1;
prev = n % 10;
n = int(n / 10);
continue;
prev = n % 10;
n = int(n / 10);
else:
# check if we have same digit
# as the previous digit
if(prev == n % 10):
flag = False;
break;
# check if the digit is greater
# than the previous one
# If true, then break from the
# loop as we are in descending
# order part
if(prev < n % 10):
flag = False;
break;
prev = n % 10;
n = int(n / 10);
return flag;
# Driver code
n = 123454321;
if(isCorrectOrder(n)):
print("YES");
else:
print("NO");
# This Code is contributed by mits
C#
// C# program to check if the
// digits are in the given order
using System;
class GFG
{
// Check if the digits follow
// the correct order
static bool isCorrectOrder(int n)
{
bool flag = true;
// to store the previous digit
int prev = -1;
// pointer to tell what type of
// sequence are we dealing with
int type = -1;
while(n != 0)
{
if(type == -1)
{
if(prev == -1)
{
prev = n % 10;
n = n / 10;
continue;
}
// check if we have same digit
// as the previous digit
if(prev == n % 10)
{
flag = false;
break;
}
// checking the peak point
// of the number
if(prev > n % 10)
{
type = 1;
prev = n % 10;
n = n / 10;
continue;
}
prev = n % 10;
n = n / 10;
}
else
{
// check if we have same digit
// as the previous digit
if(prev == n % 10)
{
flag = false;
break;
}
// check if the digit is greater
// than the previous one
// If true, then break from the
// loop as we are in descending
// order part
if(prev < n % 10)
{
flag = false;
break;
}
prev = n % 10;
n = n / 10;
}
}
return flag;
}
// Driver code
public static void Main ()
{
int n = 123454321;
if(isCorrectOrder(n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by Shashank
PHP
$n % 10)
{
$type = 1;
$prev = $n % 10;
$n = (int)$n / 10;
continue;
}
$prev = $n % 10;
$n = (int)$n / 10;
}
else
{
// check if we have same digit
// as the previous digit
if($prev == $n % 10)
{
$flag = false;
break;
}
// check if the digit is greater
// than the previous one
// If true, then break from the
// loop as we are in descending
// order part
if($prev < $n % 10)
{
$flag = false;
break;
}
$prev = $n % 10;
$n = (int) $n / 10;
}
}
return $flag;
}
// Driver code
$n = 123454321;
if(isCorrectOrder($n))
echo "YES";
else
echo "NO";
// This Code is contributed by ajit
?>
Javascript
输出:
YES