如何检查整数是否排序(双方)

时间:2015-11-29 12:30:40

标签: c function

我设法检查整数是否从右向左排序,但我如何从左到右检查?也许我应该做两个功能,一个从左到右检查,另一个检查从右到左?例如,我用第二个函数替​​换return 0;,该函数检查它是否从左到右排序。我相信这会奏效。但有没有另一种方法可以从两端检查而不使用其他功能?

已排序的数字示例:

1234
4321
4332
2334

并且没有排序:

1232
4325
4334
2332

5 个答案:

答案 0 :(得分:4)

此功能检查两个方向:

int IsDigitsInNumberSorted( int num )
{
    int leftToRight = 0;
    int rightToLeft = 9;
    while( num > 0 ) {
        const int digit = num % 10;
        num = num / 10;
        if( leftToRight <= digit ) {
            leftToRight = digit;
        } else {
            leftToRight = 10;
            if( rightToLeft == -1 ) {
                return 0;
            }
        }
        if( digit <= rightToLeft ) {
            rightToLeft = digit;
        } else {
            rightToLeft = -1;
            if( leftToRight == 10 ) {
                return 0;
            }
        }
    }
    return 1;
}

答案 1 :(得分:2)

您可以使用其他delta来检查数字是否按相同顺序更改。

实施例,

#include <stdio.h>

int digitSorted(int num)
{
    int i, rightDigit, nextDigit;
    int delta_1 = 0, delta_2 = 0;
    rightDigit = num % 10;
    num /= 10;
    while(num != 0)
    {
        nextDigit = num % 10;
        delta_2 = nextDigit - rightDigit; // << what is the difference between two adjacent digits
        if( delta_1 * delta_2 < 0 )  // << if direction changes, then it is not sorted
            return 0;
        rightDigit = nextDigit;
        num /= 10;
        if( delta_2 != 0 )
            delta_1 = delta_2;    // << save previous change
    }
    return 1;
}

int main()
{
    int x = 12345;
    printf( "x = %d, sorted = %d\n", x, digitSorted(x) );

    int y = 123453;
    printf( "y = %d, sorted = %d\n", y, digitSorted(y) );

    int z = 54321;
    printf( "z = %d, sorted = %d\n", z, digitSorted(z) );

}

输出:

x = 12345, sorted = 1
y = 123453, sorted = 0
z = 54321, sorted = 1

答案 2 :(得分:1)

这个怎么样?

如果您愿意使用c ++方式,此解决方案将适用于您。

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>

struct Comp{
    Comp():result(true),c_prev(0){}
    bool result;
    char c_prev;
    void operator()(char& c){
        if(c_prev == 0){
            c_prev=c;
            return;
        }
        if(!isdigit(c)) //to handle '-' or '+'
            return;
        result &= c_prev<=c;
        c_prev = c;
    }
};

bool digitSorted(int num, bool left2right=true){
    std::ostringstream sst; sst<<num;
    std::string str = sst.str();
    Comp result;
    if(left2right)
        result = std::for_each(str.begin(), str.end(), Comp());
    else
        result = std::for_each(str.rbegin(), str.rend(), Comp());

    return result.result;
}

答案 3 :(得分:0)

你不能用你给出的相同功能来做,但我找到的简单解决方案是使用一个标志变量。除了检查递增顺序检查递减顺序之外,还为它们提供不同的标志值并返回标志。这样做。

答案 4 :(得分:-2)

解决方案:

#include <stdio.h>
#include <conio.h>
#include <math.h>


int leftToRight(int num);
int RightToLeft(int num);
int isSorted = 0;

int main()
{

    int num1; //Variable decleration
    printf("Please enter a number: "); //output a string
    scanf("%ld", &num1); //input a number
    leftToRight(num1);
    if (isSorted == 0)
        printf("It is sorted");
    else
        printf("It is not sorted.");

    getch();
}

int leftToRight(int num)
{
    int rightDigit, nextDigit;
    rightDigit = num % 10;
    int temp = num;
    num /= 10;
    while (num != 0)
    {
        nextDigit = num % 10;
        if (nextDigit > rightDigit)
            isSorted--;
        rightDigit = nextDigit;
        num /= 10;
    }
    if (isSorted == 0)
        return isSorted;
    else
         RightToLeft(temp);
}

int RightToLeft(int num)
{
    isSorted = 0;
    int rightDigit, nextDigit;
    rightDigit = num % 10;
    num /= 10;
    while (num != 0)
    {
        nextDigit = num % 10;
        if (nextDigit < rightDigit)
            isSorted--;
        rightDigit = nextDigit;
        num /= 10;
    }
    return isSorted;
}