题解 | #计算某字符出现次数#

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37

#include <stdio.h>
#include <string.h>

int hashTable[26 * 10 * +1];
int hashFuns(char a) {
    int id = 0;
    if (a >= 'a' && a <= 'z') {
        id = id * 37 + (a - 'a');
    } else if (a >= '0' && a <= '9') {
        id = id * 63 + (a - '0') + 36;
    } else {
        id = id * 63 + (a - ' ') + 46;
    }

    return id;
}

int main() {
    char str[1005];
    char a ;
    fgets(str , 1004 , stdin);
    scanf("%c" , &a);
    int len = strlen(str);

    for (int i = 0; i < len; i++) {
        int id = 0;
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        id = hashFuns(str[i]);
        hashTable[id]++;

    }

    if (a >= 'A' && a <= 'Z') {
		a = a + 32;
	}
    int dd = hashFuns(a);
    printf("%d", hashTable[dd]);

    return 0;
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-08 20:03
已编辑
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务