C++ Primer学习笔记一

/*
题目要求把字符串BRGBBGRRGBBGBBBGRRGBGRG按RGB顺序排列,空间复杂度为O(1)
*/
#include<iostream> using namespace std; int main() { char a[]="BRGBBGRRGBBGBBBGRRGBGRG\0"; for(int i=0;i<strlen(a);i++) { cout<<a[i]<<","; } cout<<"\n"; 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; }

  

posted @ 2013-06-27 18:02  kkun  阅读(582)  评论(0编辑  收藏  举报