Mode (why only one group of data can be input, and m groups of data cannot be input). Why do we need to focus on mode analysis?
Description
The so-called mode number is the maximum number of occurrences of a given multiple set containing N elements in S,
The element with the largest number of duplicates in multiple sets of S is the mode. For example, if S = {1, 2, 2, 3, 5}, then the mode number of multi-Union S is 2, and its weight is 3.
Now, your task is to calculate the mode and its weight of S for a given multi-duplicated set S composed of m natural numbers.
-
Input
-
The first action n indicates the number of test data groups. (N <30)
The first row of each group of tests is an integer m, indicating that the number of elements in the Multi-replica set S is m
In the next row, the m (m <100) natural numbers not greater than 0.1 million are given.
(The number of occurrences of different elements is not the same, for example, S = {11, 11 }).
-
Output
-
Each group of test data outputs a row, which contains two numbers. The first is the mode, the second is its weight, and the middle is separated by spaces.
-
Sample Input
-
161 2 2 2 3 5
-
Sample output
-
2 3
My program:
# Include <iostream>
# Include <vector>
Using namespace std;
Struct num
{
Int value;
Int count;
};
Int main ()
{
Int m;
Vector <int>;
Vector <num> B;
Cin> m;
For (int I = 0; I <m; I ++)
{
Int n;
Cin> n;
For (int j = 0; j <n; j ++)
{
Int x;
Cin> x;
A. push_back (x );
}
Num c = {0, 0}; // indicates the number of 0
For (j = 0; I <n; I ++)// Incorrect. I is replaced with j
{
If (a [j] = 0)
C. count ++;
}
For (j = 0; j <n; j ++)
{
Int x = 1; // indicates the number of times a [j] appears, because when compared, a [j] is also counted once, so x = 1;
If (a [j]! = 0)
{
For (int k = j + 1; k <n; k ++)
{
If (a [k] = a [j])
{
X ++;
A [k] = 0;
}
}
If (x> c. count)
{
C. count = x;
C. value = a [j];
}
A [j] = 0;
}
}
B. push_back (c );
A. clear ();
}
For (I = 0; I <m; I ++)
Cout <B [I]. value <"" <B [I]. count <endl;
Return 0;
}
# Include <stdio. h>
Int main ()
{
Int n, m, I, k, a [100], max, sum, j;
Scanf ("% d", & n );
While (n --)
{
Sum = 1;
Scanf ("% d", & m );
For (I = 0; I <m; I ++)
Scanf ("% d", & a [I]);
For (j = 0, k = 0; j <m; j ++)
{
For (I = 0; I <m; I ++)
{
If (a [I] = a [j]) k ++;
If (k> sum)
{
Sum = k;
Max = a [I];
}
}
K = 0;
}
Printf ("% d \ n", max, sum );
}
Return 0;
} (Correct solution)