循环里面用连续的三个if便满足题意,用三个计数器计Z、O、J的个数
#include<stdio.h>
#include<string.h>
int main()
{
char str[105];
int nz = 0,no = 0,nj = 0;
int flag = 0;
gets(str);
for(int i = 0;i<strlen(str);i++)
{
if(str[i] == 'Z')
nz ++;
else if(str[i] == 'O')
no ++;
else
nj ++;
}
for(int i = 0;i<strlen(str);)
{
if(nz-- >0)
{
printf("Z");
i++;
}
if(no-->0)
{
printf("O");
i++;
}
if(nj-- >0)
{
printf("J");
i++;
}
}
return 0;
}
京公网安备 11010502036488号