C++ Primer学习笔记一

时间:2022-05-19 19:15:16
/*
题目要求把字符串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;
}