fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5. int N;
  6. scanf("%d", &N);
  7.  
  8. char word[N][51]; // 단어를 저장할 2차원 배열
  9. int len[N]; // 단어의 길이를 저장할 배열
  10.  
  11. // 단어와 길이 입력받기
  12. for (int i = 0; i < N; i++) {
  13. scanf("%s", word[i]);
  14. len[i] = strlen(word[i]);
  15. }
  16.  
  17. // 정렬
  18. for (int i = 0; i < N - 1; i++) {
  19. for (int j = i + 1; j < N; j++) {
  20. // 길이가 짧은 것부터 정렬
  21. if (len[i] > len[j] || (len[i] == len[j] && strcmp(word[i], word[j]) > 0)) {
  22. // 문자열의 스왑
  23. char temp[51];
  24. strcpy(temp, word[i]);
  25. strcpy(word[i], word[j]);
  26. strcpy(word[j], temp);
  27.  
  28. // 길이의 스왑
  29. int temp_len = len[i];
  30. len[i] = len[j];
  31. len[j] = temp_len;
  32. }
  33. }
  34. }
  35.  
  36. // 정렬된 단어 출력
  37. for (int i = 0; i < N; i++) {
  38. printf("%s\n", word[i]);
  39. }
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 5308KB
stdin
13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours
stdout
i
im
it
no
no
but
more
more
wait
wont
yours
cannot
hesitate