View Code of Problem 126

#include "stdio.h"
#include "math.h"
#include "string.h"


int main()
{	
	int i,j;
	char s[101][51];
	int n,m,k,temp;
	char ss[51];

	for(;scanf("%d%d",&n,&m)!=EOF;)
	{
		int count[101]={0};
		for(i=0;i<m;i++)
		{			
			fflush(stdin);
			for(j=0;j<n;j++)
				scanf("%c",&s[i][j]);
			s[i][j]='\0';
		}								//for(i=0;i<m;i++)puts(s[i]);

		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				for(k=j+1;k<n;k++)
				{
					if(s[i][j]>s[i][k])
						count[i]++;
				}
			}
		}							//	for(i=0;i<m;i++)	printf("%d ",count[i]);

		for(i=0;i<m;i++)
		{
			for(j=0;j<m-i-1;j++)
			{
				if(count[j]>count[j+1])
				{
					temp=count[j];count[j]=count[j+1];count[j+1]=temp;
					strcpy(ss,s[j]);strcpy(s[j],s[j+1]);strcpy(s[j+1],ss);
				}
			}
		}
		for(i=0;i<m;i++)
			puts(s[i]);
	}
}

Double click to view unformatted code.


Back to problem 126