trrb13
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
char a[100];
int i,n,x,j,dem,i1,i2,kt;
//void output();
void daytang(int n);
void print();
main()
{ clrscr();
printf("Bai 1.3:
Viet chuong trinh nhap vao xau chu gom n\chu cai hoa(A...Z)
Trong do co mot so chu cai lap lai.
Liet ke ta ca cac cach sap xep n chu cai nay.
Co dem tong so cach sap xep
");
do{
printf("Nhap vao xau:");
gets(a);
n=strlen(a);
kt=1;
for(i=0;i<n;i++)
if((a[i]<65)||(a[i]>90))
kt=0;
if((kt==0)||(n==0))
{ printf("
Loi nhap sai! Moi nhap lai");
getch();
}
}while((kt==0)||(n==0));
printf("
Ket qua:");
printf("
Xau vua nhap la: %s",a);
daytang(n);
dem=1;
printf("
");
printf("%4d:",dem);
print();
while(i1<n)
{ i1=1;
while((a[i1-1]<=a[i1])&&(i1<=n))
i1++;
i2=0;
while((a[i2]<=a[i1])&&(i2<i1))
i2++;
x=a[i1];
a[i1]=a[i2];
a[i2]=x;
daytang(i1);
if(i1<n)
{dem++;
printf("%4d:",dem);
print();
if(dem%4==0)
printf("
");
}
}
printf("
Ket qua: So hoan vi khong lap la %d",dem);
getch();
}
void daytang(int n)
{ for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(a[i]<a[j])
{x=a[i];
a[i]=a[j];
a[j]=x;
}
}
void print()
{
printf("%s",a);
}
Bạn đang đọc truyện trên: AzTruyen.Top