fork(1) download
  1. #include <stdio.h>
  2. int find_min(int a,int b){
  3. if (a > b){
  4. return b;}
  5. else {
  6. return a;}}
  7.  
  8. int find_intersection_with_count(int *arr1,int *arr2,int n1,int n2){
  9. int count1[10] = {0};
  10. int count2[10] = {0};
  11. int count[10] = {0};
  12. for (int i = 0;i <= 9;i ++){
  13. for (int j = 0;j < n1;j ++){
  14. if(*(arr1 + j) == i){
  15. count1[i] ++;}}
  16. for (int j = 0;j < n2;j ++){
  17. if(*(arr2 + j) == i){
  18. count2[i] ++;}}
  19. count[i] = find_min(count1[i],count2[i]);
  20. if (count[i] != 0){
  21. printf("%d (%d) ",i,count[i]);}}
  22. printf("\n");
  23. for (int k = 9;k >= 0 ;k --){
  24. if (count[k] != 0){
  25. printf("%d (%d) ",k,count[k]);}}}
  26.  
  27. int main(void) {
  28. int n1,n2;
  29. scanf("%d",&n1);
  30. int arr1[n1];
  31. for (int i = 0;i < n1;i ++){
  32. scanf("%1d",&arr1[i]);}
  33. scanf("%d",&n2);
  34. int arr2[n2];
  35. for (int i = 0;i < n2;i ++){
  36. scanf("%1d",&arr2[i]);}
  37. find_intersection_with_count(&arr1,&arr2,n1,n2);
  38. return 0;}
Success #stdin #stdout 0.01s 5272KB
stdin
10
5 7 8 8 1 8 5 8 7 8
6
5 7 8 8 8 8
stdout
5 (1) 7 (1) 8 (4) 
8 (4) 7 (1) 5 (1)