1610

来源:互联网 发布:笔记本电脑推荐知乎 编辑:程序博客网 时间:2024/06/05 05:35

紫书上习题8-2;将字符串由小到大排列,找出中间两个;1,可以根据小的和大的对比求出(老是wrong) 2,可以根据for循环组成一个字符串然后判断;

#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>using namespace std;const int N = 1005;string str[N];string res;int n;int main()  {    while(scanf("%d",&n) && n) {        getchar();        for(int i = 0; i < n; i++) {            getline(cin ,str[i]);        }        sort(str, str + n);        string a = str[n / 2 - 1];        string b = str[n / 2];        res = "";        for(int i = 0; i < a.size() && i < b.size(); i++) {            if(a[i] == b[i])                res += a[i];            else {                if(i == a.size() - 1) {                    res += a[i];                }else if((b[i] - a[i] > 1) || i != b.size() - 1) {                    res += (a[i] + 1);                }else {                    res += a[i];                    for(int j = i + 1; j < a.size(); j++) {                        if(j == a.size() - 1) {                            res += a[j];                            break;                        }                        if(a[j] != 'Z') {                            res += (a[j] + 1);                            break;                        }                        res += 'Z';                    }                }                break;            }        }        cout << res << endl;    }}


0 0
原创粉丝点击