Test instructions: Enter the color of n balloons (the color of the balloon is a string, up to 15 lowercase letters), output the color of the same color balloon, enter the 0 end program.
A balloon that guarantees that each group of data has a unique and up to one color.
Thinking: Using arrays of type string to save the color of n balloons
Examples:
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) Each element in the 2 is compared to each element in the cnt++, and the maximum number of elements is eventually saved in Word.
The subscript that records the maximum number of elements, stored in flag.
<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <string>using namespace std;int main (int argc, char *argv[]) {int n;string An array of type color[1010];//string while (1) {string color[1010] = {""};//initialized to an empty array without this initialization will 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++;//element equals + +}}if (cnt > Word) {word = cnt;//The maximum number of elements saved in Word flag = i;//The index of the maximum number of elements}}cout << Color[flag] <&l T Endl }return 0;} </span>
Hdu_1002_let the Balloon Rise (analog)