比较两个版本字符串version1和version2

时间限制:1秒

空间限制:32768K

如果version1 > version2 返回1,如果 version1 < version2 返回-1,不然返回0.

输入的version字符串非空,只包含数字和字符.。.字符不代表通常意义上的小数点,只是用来区分数字序列。例如字符串2.5并不代表二点五,只是代表版本是第一级版本号是2,第二级版本号是5.

输入描述:
两个字符串,用空格分割。
每个字符串为一个version字符串,非空,只包含数字和字符.

输出描述:
只能输出1, -1,或0

输入例子1:
0.1 1.1

输出例子1:
-1

# include 
# include 
# include 
using namespace std;


int main(){
    string a,b;
    char num[100000];
    int x,y,count_a,count_b,len;
    int A[100000],B[100000];
    int status;
    while (cin>>a>>b) {
        x = 0,y=0,count_a=0;
        for (int i=0; i < a.length(); i++) {
            if (a[i] == '.') {
                count_a++;
                num[x] = '\0';
                A[y++] = atoi(num);
                x=0;
                continue;
            }
            num[x++] = a[i];
        }
        num[x] = '\0';
        A[y++] = atoi(num);
        
        x = 0,y=0,count_b=0;
        for (int i=0; i < b.length(); i++) {
            if (b[i] == '.') {
                count_b++;
                num[x] = '\0';
                B[y++] = atoi(num);
                x=0;
                continue;
            }
            num[x++] = b[i];
        }
        num[x] = '\0';
        B[y++] = atoi(num);
        //
        count_a++;count_b++;
        len = count_a > count_b? count_b:count_a;
        status = 0;
        for (int i=0; i B[i]){
                status =  1;
                break;
            }
        }
        if (status == 0 && count_a != count_b) {
            status = count_a>count_b?1:-1;
        }
        printf("%d\n",status);
    }
    return  0;
}

你可能感兴趣的:(数据结构与算法)