Jun 9, 2018 at 3:09am UTC
This code orders all letters E to the left (esquerda in portuguese), and all letters D to the right (direita in portuguese). I want to stop the cout command when the line is ordered, but it keeps printing the same line.
imput: 8 DEEEDDDE
output:
DEEEDDDE
EDEEDDED
EEDEDEDD
EEEDEDDD
EEEEDDDD
EEEEDDDD
EEEEDDDD
EEEEDDDD
It should stop in the fifth line. Any help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
char fila[n];
for (int i=0;i<n;i++)
cin >> fila[i];
char aux;
bool ordenado=false ;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
cout << fila[j];
}
cout << endl;
for (int l=0;l<n;l++)
if ((fila[l]=='D' && fila[l+1]=='D' ) && (fila[l]=='E' && fila[l-1]=='E' ))
ordenado=true ;
if (ordenado)
break ;
for (int k=0;k<n;k++){
if (k!=n-1 && fila[k]=='D' && fila[k+1]=='E' ){
aux=fila[k+1];
fila[k+1]=fila[k];
fila[k]=aux;
k++;
}
}
}
return 0;
}
Last edited on Jun 9, 2018 at 3:53am UTC
Jun 9, 2018 at 3:52am UTC
Thanks for the help, but I need the answer on my code, not another code using libraries that I didn't use before
Last edited on Jun 9, 2018 at 3:58am UTC