fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5. int N, X;
  6.  
  7. // Input the number of sticker types
  8. scanf("%d", &N);
  9.  
  10. // Declare an array to store sticker counts
  11. int counts[N];
  12.  
  13. // Input sticker counts for each type
  14. for (int i = 0; i < N; i++) {
  15. scanf("%d", &counts[i]);
  16. }
  17.  
  18. // Input the categorization factor
  19. scanf("%d", &X);
  20.  
  21. // Sort sticker counts based on remainders using bubble sort
  22. for (int i = 0; i < N - 1; i++) {
  23. for (int j = 0; j < N - i - 1; j++) {
  24. int mod_a = counts[j] % X;
  25. int mod_b = counts[j + 1] % X;
  26. if (mod_a > mod_b) {
  27. // Swap counts[j] and counts[j + 1]
  28. int temp = counts[j];
  29. counts[j] = counts[j + 1];
  30. counts[j + 1] = temp;
  31. }
  32. }
  33. }
  34.  
  35. // Print the refined arrangement of sticker counts
  36. for (int i = 0; i < N; i++) {
  37. printf("%d ", counts[i]);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5272KB
stdin
6
129 880 694 199 345 248
2
stdout
880 694 248 129 199 345