fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int compare(const void *a, const void *b) {
  5. return (*(int *)a - *(int *)b);
  6. }
  7.  
  8. int main() {
  9. int numbers[20], n = 0, num;
  10. char input[10];
  11.  
  12. // รับค่าตัวเลขจนกว่าจะใส่ "stop"
  13. while (1) {
  14. scanf("%s", input);
  15.  
  16. // ตรวจสอบการหยุดการรับข้อมูล
  17. if (input[0] == 's' && input[1] == 't' && input[2] == 'o' && input[3] == 'p' && input[4] == '\0') {
  18. break;
  19. }
  20.  
  21. num = atoi(input); // แปลงสตริงเป็นตัวเลข
  22.  
  23. // ตรวจสอบว่ามีตัวเลขซ้ำหรือไม่
  24. int is_duplicate = 0;
  25. for (int i = 0; i < n; i++) {
  26. if (numbers[i] == num) {
  27. is_duplicate = 1;
  28. break;
  29. }
  30. }
  31.  
  32. // ถ้าไม่มีตัวเลขซ้ำ ให้เพิ่มลงใน array
  33. if (!is_duplicate) {
  34. numbers[n++] = num;
  35. }
  36. }
  37.  
  38. // เรียงลำดับตัวเลข
  39. qsort(numbers, n, sizeof(int), compare);
  40.  
  41. // แสดงผลลัพธ์ที่เรียงแล้ว
  42. for (int i = 0; i < n; i++) {
  43. printf("%d ", numbers[i]);
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5284KB
stdin
 4 5 7 6 9 stop 20
stdout
4 5 6 7 9