View Code of Problem 126

#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
#include "malloc.h"
int main()
{
	int n,m,i,j,k,t;
	
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		char a[101][101];
		int *b=(int *)malloc(m*sizeof(int));
		memset(b,0,sizeof(int));
		for(i=0;i<m;i++)
		  scanf("%s",a[i]);
		  
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				for(k=j+1;k<n;k++)
				{
					if(a[i][j]>a[i][k])
					   b[i]++;
				}
			}
		}
		for(i=0;i<m-1;i++)
		{
			for(j=0;j<m-1;j++)
			{
				if(b[j]>b[j+1])
				{
					t=b[j];
					b[j]=b[j+1];
					b[j+1]=t;
					char *temp=(char *)malloc(100*sizeof(char));
					strcpy(temp,a[j]);
					strcpy(a[j],a[j+1]);
					strcpy(a[j+1],temp);
				}
			}
		}
		for(i=0;i<m;i++)
		  printf("%s\n",&a[i]);
	}
    return 0;
}

Double click to view unformatted code.


Back to problem 126