2302 - 气球MM
每到ACM大赛,最忙碌的当数热心的送气球MM了;但是因为送的气球实在太多了,气球MM往往会送错,也就是某位同学做对了一道题,总是会收到很多个相同颜色的气球(一个就够了),不过送错也没关系,因为热心的你肯定会帮气球MM改正错误的;现在,她需要你帮她写一个程序,计算出某位同学应该得到气球的正确数目。
注意:气球颜色只有6种,R(红),G(绿),B(蓝),W(白),Y(黄),O(橙)
题目输入
第一行有一个数字T,代表有T位同学;每位同学得气球的情况用一个字符串表示(长度在1到100之间),代表该位同学当前获得的气球;每个字符串中仅包含题目描述中的六个大写字母。
题目输出
对于每位同学,输出一行,为一个数字,代表该位同学应该得到的气球正确数目。
输入/输出样例
题目输入
3 RGB YYY WWOO
题目输出
3 1 2
提示
第一位同学没有被送错;
第二位同学中收到了3个一样的,所以去掉相同的两个,所以正确数目是1个。
C语言解答
#include<stdio.h> #include<string.h> main() { int n, nums[6], result; char str[200]; scanf("%d", &n); while(n--) { memset(nums, 0, sizeof(nums)); result = 0; scanf("%s", str); for(int i = 0; str[i]; i++) { if(str[i] == 'R') nums[0]++; else if(str[i] == 'G') nums[1]++; else if(str[i] == 'B') nums[2]++; else if(str[i] == 'W') nums[3]++; else if(str[i] == 'Y') nums[4]++; else if(str[i] == 'O') nums[5]++; } for(int i = 0; i < 6; i++) if(nums[i]) result++; printf("%d\n", result); } }
C++解答
#include <stdio.h> int main() { char str[105], t[6] = {'R', 'G', 'B', 'W', 'Y', 'O'}; int Tcase, i, j, ans, cnt[6]; scanf("%d", &Tcase); while(Tcase--) { for(i = 0; i < 6; i++) cnt[i] = 0; scanf("%s", str); for(i = 0; str[i] != '\0'; i++) { for(j = 0; j < 6; j++) if(t[j] == str[i]) cnt[j]++; } ans = 0; for(i = 0; i < 6; i++) if(cnt[i] > 0) ans++; printf("%d\n", ans); } return 0; }
提示
第一位同学没有被送错;
第二位同学中收到了3个一样的,所以去掉相同的两个,所以正确数目是1个。