zoukankan      html  css  js  c++  java
  • 算法提高:比较字符串

      
    资源限制
    时间限制: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 }
  • 相关阅读:
    android Serializable 和 Parcelable 区别
    Android HttpClient 用法以及乱码解决
    android 头像选择以及裁剪
    播放动画
    跑马灯效果
    Paint基本属性
    安卓开发中的各种事件
    View类和surfaceView详细介绍
    hadoop资源
    wsdl文件转换为java
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/12530420.html
Copyright © 2011-2022 走看看