整忘了 截止了作业三 就没有题目了
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
string x;
while(cin>>n>>x&&n)
{
string res;
int idx1=0,idx2=n-1;
while(n--)
{
if(x[idx1]<x[idx2])
{
res.push_back(x[idx1]);
idx1++;
}
else if(x[idx1]>x[idx2])
{
res.push_back(x[idx2]);
idx2--;
}
else{
int k=idx1,k2=idx2;
while(k!=k2&&x[k]==x[k2])
{
k++,k2--;
}
if(x[k]<x[k2]) res.push_back(x[idx1++]);
else res.push_back(x[idx2--]);
}
}
cout<<res<<endl;
}
}
这是一个C++程序,它读取一个整数n和一个字符串x,然后对字符串进行排序。程序使用双指针技术,从两端向中间遍历,根据字符大小将它们推入结果字符串res中。最后输出排序后的字符串。
1365

被折叠的 条评论
为什么被折叠?



