View Code of Problem 126

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>



int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	/*char **a = (char **)malloc((m+1) * sizeof(char *));
	for (int i = 0;i < m;i++) {
		a[i] = (char *)malloc((n+1) * sizeof(char));
	}*/
	int *b = (int *)malloc(m * sizeof(int));
	for (int i = 0;i < m;i++) {
		b[i] = 0;
	}
	char a[100][100];
	for (int i = 0;i < m;i++) {
		scanf_s("%s",a[i]);
	}
	for (int i = 0;i < m;i++) {
		for (int j = 0;j < n;j++) {
			for (int k = j + 1;k < n;k++) {
				if (a[i][j]>a[i][k])
					b[i]++;
			}
		}
	}
	for (int i = m-1;i>0;i--) {
		int flag = 1;
		for (int j = 0;j < i;j++) {
			if (b[j] > b[j + 1]) {
				int temp = b[j];
				b[j] = b[j + 1];
				b[j + 1] = temp;
				char *t = (char *)malloc(100 * sizeof(char));
				strcpy(t, a[j]);
				strcpy(a[j], a[j + 1]);
				strcpy(a[j + 1], t);
			}
		}

	}
	
	for (int i = 0;i < m;i++) {
		printf("%s\n", a[i]);
	}


}

/*
Main.c: In function 'main':
Main.c:22:3: warning: implicit declaration of function 'scanf_s' [-Wimplicit-function-declaration]
   scanf_s("%s",a[i]);
   ^
Main.c:33:7: warning: unused variable 'flag' [-Wunused-variable]
   int flag = 1;
       ^
/tmp/ccR8xw0L.o: In function `main':
Main.c:(.text+0xa9): undefined reference to `scanf_s'
collect2: error: ld returned 1 exit status
*/

Double click to view unformatted code.


Back to problem 126