以下是修改后的代码:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int N;
    cin >> N;
    string s;
    for (int i = 0; i < N; i++) {
        char c;
        cin >> c;
        s.push_back(c);
    }
    
    string ans;
    int left = 0, right = N - 1;
    while (left <= right) {
        bool flag = false;
        for (int i = 0; left + i <= right; i++) {
            if (s[left + i] < s[right - i]) {
                flag = true;
                break;
            }
            if (s[left + i] > s[right - i]) {
                flag = false;
                break;
            }
        }
        if (flag) {
            ans.push_back(s[left]);
            left++;
        } else {
            ans.push_back(s[right]);
            right--;
        }
    }
    reverse(ans.begin(), ans.end());
    
    for (int i = 0; i < N; i++) {
        cout << ans[i];
        if ((i + 1) % 80 == 0) {
            cout << endl;
        }
    }
    
    return 0;
}

修改后的代码使用了更简洁的实现来解决问题。首先,将输入的字符存储在一个字符串中。然后,使用双指针的方式从字符串的两端开始比较字符。如果左边的字符小于右边的字符,就将左边的字符加入答案字符串中,并移动左指针;如果左边的字符大于右边的字符,就将右边的字符加入答案字符串中,并移动右指针。最后,将答案字符串反转并输出。同时,根据题目要求,每输出80个字符就换行一次


原文地址: https://www.cveoy.top/t/topic/izC7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录