Hdu_1002_Let the Balloon Rise (simulation)
Enter the color of n balloons (the color of the balloon is a string with a maximum of 15 lowercase letters), and output the color of the balloon with the same color at most. Enter 0 to end the program.
Ensure that each group of data has a unique and most colored balloon.
Solution: Use a string array to save the colors of n balloons
Example:
1) a B a d e
2) a B a d e
A-> a B a d e
B-> a B a d e
C-> a B a d e
D-> a B a d e
1) compare each element in and 2). If they are equal, cnt ++ stores the maximum number of elements in word.
The subscript of the maximum number of elements in the record, which is stored in the flag.
# Include
# Include
Using namespace std; int main (int argc, char * argv []) {int n; string color [1010]; // string type array while (1) {string color [1010] ={}; // array whose Initialization is empty. if it is not initialized, WA cin >>> n; if (n = 0) return 0; for (int I = 0; I <n; I ++) {cin> color [I];} int cnt, flag = 10000; intword = 0; for (int I = 0; I <n; I ++) {cnt = 0; for (int j = 0; j <n; j ++) {if (color [I] = color [j]) {cnt ++; // if the element is equal, ++} if (cnt> word) {word = cnt; // The maximum number of elements is saved in word. flag = I; // The subscript that records the maximum number of elements }}cout <color [flag] <endl;} return 0 ;}