资源限制
时间限制:1.0s 内存限制:512.0MB
编程实现两个字符串s1和s2的字典序比较。(保证每一个字符串不是另一个的前缀,且长度在100以内)。若s1和s2相等,输出0;若它们不相等,则指出其第一个不同字符的ASCII码的差值:如果s1>s2,则差值为正;如果s1<s2,则差值为负。
样例输入
java basic
样例输出
8
1 #include<stdio.h> 2 #include<string.h> 3 4 int main(void) 5 { 6 char str[2][100] = {0}; 7 int i,j = -1; 8 int tem; 9 10 11 for (i = 0; i < 2; i++) //为了题目给的输入格式只能这样搞了 12 { 13 while (1) 14 { 15 scanf("%c", &str[i][++j]); 16 if (str[i][j] == ' ' || str[i][j] == ' ') 17 { 18 str[i][j] = 0; //防止第二个字符串吸收' ' 19 j = -1; 20 break; 21 } 22 } 23 } 24 25 if (strcmp(str[0], str[1]) == 0) 26 { 27 printf("0"); 28 } 29 else if (strcmp(str[0], str[1]) > 0) 30 { 31 for (i = 0; i < strlen(str[0]); i++) 32 { 33 if (str[0][i] != str[1][i]) 34 { 35 break; 36 } 37 } 38 printf("%d", (int)(str[0][i] - str[1][i])); 39 } 40 else if (strcmp(str[0], str[1]) < 0) 41 { 42 for (i = 0; i < strlen(str[1]); i++) 43 { 44 if (str[0][i] != str[1][i]) 45 { 46 break; 47 } 48 } 49 printf("%d", (int)(str[0][i] - str[1][i])); 50 } 51 52 return 0; 53 }