Advertisement
ppupil2

PE 04.01.20 Q7

Mar 17th, 2022
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int indexOf(int a, int numbers[], int size) {
  7.     int i;
  8.    
  9.     for (i = 0; i < size; i++) {
  10.         if (numbers[i] == a) {
  11.             return i;
  12.         }
  13.     }
  14.     return -1;
  15. }
  16.  
  17. int main() {
  18.   system("cls");
  19.   //INPUT - @STUDENT:ADD YOUR CODE FOR INPUT HERE:
  20.     int i, a[7];
  21.     for(i = 0; i < 7; i++) {
  22.         scanf("%d", &a[i]);
  23.     }
  24.   // Fixed Do not edit anything here.
  25.   printf("\nOUTPUT:\n");
  26.   //@STUDENT: WRITE YOUR OUTPUT HERE:
  27.     int numbers[7];
  28.     int count[7];
  29.     int size = 0;
  30.    
  31.     int index, max, next;
  32.     for(i = 0; i < 7; i++) {
  33.         if (a[i] >= 100 && a[i] <= 999) { // so co 3 chu so
  34.             index = indexOf(a[i], numbers, size);
  35.             if (index != -1) { // tim thay ~> tang count 1
  36.                 count[index]++;
  37.             } else {
  38.                 numbers[size] = a[i];
  39.                 count[size] = 1;
  40.                 size++;
  41.             }
  42.         }
  43.     }
  44.    
  45.     if (size == 0) {
  46.         printf("No three-digit number");
  47.     } else {
  48.         // tim so lan xuat hien nhieu` nhat'
  49.         max = -1;
  50.         for(i = 0; i < size; i++) {
  51.             if (count[i] > max) {
  52.                 max = count[i];
  53.             }
  54.         }
  55.        
  56.         next = 0; // index tiep theo sau khi in so dau tien
  57.         for(i = 0; i < size; i++) {
  58.             if (count[i] == max) {
  59.                 printf("%d", numbers[i]);
  60.                 next = i;
  61.                 break;
  62.             }
  63.         }
  64.         for(i = next+1; i < size; i++) {
  65.             if (count[i] == max) {
  66.                 printf(" %d", numbers[i]);
  67.             }
  68.         }
  69.     }  
  70.    
  71.    
  72.   //--FIXED PART - DO NOT EDIT ANY THINGS HERE
  73.   printf("\n");
  74.   system ("pause");
  75.   return(0);
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement