http://acm.xidian.edu.cn/problem.php?id=1124
找第一个不是当前到最后的最大值的元素,和这后面的最大的元素交换。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; char a[1005]; int main() { while(~scanf("%s",a)) { int len = strlen(a),first = 0,last = 0,now = 0; while(now < len) { for(int i = now+1;i < len;i++) { first = last = now; char maxx = a[now]; if(a[i] > maxx) { maxx = a[i]; last = i; } } if(first != last) break; for(now++;a[now] == a[now-1];now++); } swap(a[first],a[last]); printf("%s\n",a); } return 0; }
时间: 2024-10-06 00:07:45