HDU 1219 AC Me(小技巧)
2017-12-25 14:25
387 查看
不要傻傻的用26个case或者if去做
不要傻傻的用26个case或者if去做
不要傻傻的用26个case或者if去做
换个思路,开一个大小26的int数组,从第一个元素开始分别代表'a','b'.....'z'的个数
ch[i]-'a'就是对应的元素,然后计数即可
#include<stdio.h>
#include<string.h>
char ch[100005];
int main(void)
{
while (gets(ch) != NULL) {
int letter[27] = {0};
for (int i = 0; ch[i]!='\0'; i++) {
letter[ch[i] - 'a']++;
}
char a = 'a';
for (int i = 0; i < 26; i++) {
printf("%c:%d\n", a++, letter[i]);
}
printf("\n");
}
return 0;
}
不要傻傻的用26个case或者if去做
不要傻傻的用26个case或者if去做
换个思路,开一个大小26的int数组,从第一个元素开始分别代表'a','b'.....'z'的个数
ch[i]-'a'就是对应的元素,然后计数即可
#include<stdio.h>
#include<string.h>
char ch[100005];
int main(void)
{
while (gets(ch) != NULL) {
int letter[27] = {0};
for (int i = 0; ch[i]!='\0'; i++) {
letter[ch[i] - 'a']++;
}
char a = 'a';
for (int i = 0; i < 26; i++) {
printf("%c:%d\n", a++, letter[i]);
}
printf("\n");
}
return 0;
}
相关文章推荐
- hdu 1219 AC Me
- 杭电 HDU 1219 java AC Me
- hdu 1219 AC Me
- HDU 1219 AC Me(字母统计)
- HDU 1219 AC Me
- hdu1219 AC Me
- HDU 1219 AC Me
- hdu-1219-AC Me
- hdu1219 AC Me
- HDU 1219 AC me
- HDU 1219 AC Me
- hdu-oj 1219 AC Me
- HDU 1219 AC Me
- hdu 1219 AC Me
- HDU1219 AC Me【输入输出+水题】
- hdu 1219 AC Me (java版本)
- 【hdu - 1219 AC Me】
- hdu 1219 AC Me
- hdu-1219 AC Me
- HDU-1219 AC Me (水)