/*
题目要求把字符串BRGBBGRRGBBGBBBGRRGBGRG按RGB顺序排列,空间复杂度为O(1)
*/
#include<iostream> using namespace std; int main() { char a[]="BRGBBGRRGBBGBBBGRRGBGRG "; for(int i=0;i<strlen(a);i++) { cout<<a[i]<<","; } cout<<" "; for(int i = 0;i < strlen(a);i++) { if(a[i]=='R') { for(int j=0;j < i;j++) { if(a[j]!='R') { char temp = a[j]; a[j] = a[i]; a[i] = temp; break; } } } if(a[i]=='B') { for(int j=strlen(a)-1;j>i;j--) { if(a[j]!='B') { char temp = a[j]; a[j] = a[i]; a[i] = temp; i--; break; } } } } for(int i=0;i<strlen(a);i++) { cout<<a[i]<<","; } bool signal; cin>>signal; }