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; }
------------------------------------------
除非特别声明,文章均为原创,版权与博客园共有,转载请保留出处
BUY ME COFFEE