题目 B1038 统计同成绩学生
-
题意
输入n
个学生成绩,然后输入k
个成绩进行查询,输出该成绩对应的学生人数。 -
思路
因为成绩是百分制整数,所以采用大小为101的数组下标与分数对应。输入时记录个数,查询时直接根据下标查询即可。 -
Code in C++
#include <cstdio>
int numbers[101] = {0};
int main()
{
int n;
scanf("%d", &n);
int tmp;
for (int i = 0; i < n; ++i) {
scanf("%d", &tmp);
++numbers[tmp];
}
int k;
scanf("%d", &k);
for (int i = 0; i < k; ++i) {
scanf("%d", &tmp);
if (i > 0) printf(" ");
printf("%d",numbers[tmp]);
}
return 0;
}
题目 B1039 到底买不买
-
题意
输入商家字符串和买家字符串,判断如果买家字符串都包含在商家中,则输出Yes
及多余的个数。如果不是,输出No
及缺失个数。 -
思路
我这里采用偷懒的方法直接申请了256大小的数组作为映射,然后输入商家字符串时统计各个字母的个数。在输入买家字符串时对对应字母个数进行减法,用flag
标记是否有缺失。对应字母个数为0时,即无法减时,++count
。无缺失的情况,多余个数直接是两个字符串长度之差。缺失的情况下,缺失个数就是计数count
的值。 -
Code in C++
#include <cstdio>
#include <cstring>
#define maxn 1002
int hashTable[256];
char str[maxn];
int main()
{
memset(hashTable, 0, sizeof(hashTable));
scanf("%s", str);
int olen = strlen(str);
for (int i = 0; i < olen; ++i) {
++hashTable[str[i]];
}
int count = 0;
scanf("%s", str);
bool flag = true;
int nlen = strlen(str);
for (int i = 0; i < nlen; ++i) {
if (hashTable[str[i]] == 0) {
flag = false;
++count;
} else {
--hashTable[str[i]];
}
}
if (flag) {
printf("Yes %d", olen-nlen);
} else {
printf("No %d", count);
}
return 0;
}
题目 B1042 统计字符
-
题意
输出字符串中出现频率最高的字母及其次数。 -
思路
用isalpha()
函数和tolower()
函数处理字母进行统计,用maxindex
记录最高频率字母的下标。在输入的过程中不断迭代。这里可能需要注意的就是当字母出现次数相同时按小的下标来。 -
Code in C++
#include <iostream>
#include <string>
#include <cctype>
char ch[26] = {0};
int main()
{
std::string str;
getline(std::cin, str);
int maxindex = 0;
int lenth = str.size();
for (int i = 0; i < lenth; ++i) {
if (isalpha(str[i])) {
str[i] = tolower(str[i]);
int index = str[i] - 'a';
++ch[index];
if (ch[maxindex] == ch[index]) {
if (index < maxindex) maxindex = index;
} else if (ch[maxindex] < ch[index]) {
maxindex = index;
}
}
}
printf("%c %d", maxindex+'a', ch[maxindex]);
}
题目 B1043 输出PATest
-
题意
将输入的字符串按P,A,T,e,s,t
顺序输出,个数为0的字符不用输出。 -
思路
这里对于P,A,T,e,s,t
我写了index
函数去映射数组的下标。然后采用循环的方式对P,A,T,e,s,t
这个顺序进行,当都为0
的时候就停止。这里其实也可以用个sum
变量去记总的个数,然后以sum
为条件去循环。 -
Code in C++
#include <cstdio>
#include <cstring>
#define maxn 10001
int hashTable[6] = {0};
char str[maxn];
int index(char c) {
switch(c) {
case 'P':
return 0;
case 'A':
return 1;
case 'T':
return 2;
case 'e':
return 3;
case 's':
return 4;
case 't':
return 5;
default:
return -1;
}
}
int main()
{
scanf("%s", str);
int lenth = strlen(str);
for (int i = 0; i < lenth; ++i) {
int tmp = index(str[i]);
if (tmp != -1) {
++hashTable[tmp];
}
}
while (1) {
if (hashTable[index('P')]) {
printf("P");
--hashTable[index('P')];
}
if (hashTable[index('A')]) {
printf("A");
--hashTable[index('A')];
}
if (hashTable[index('T')]) {
printf("T");
--hashTable[index('T')];
}
if (hashTable[index('e')]) {
printf("e");
--hashTable[index('e')];
}
if (hashTable[index('s')]) {
printf("s");
--hashTable[index('s')];
}
if (hashTable[index('t')]) {
printf("t");
--hashTable[index('t')];
}
if (hashTable[index('P')] == 0 && hashTable[index('A')] == 0 && hashTable[index('T')] == 0 && hashTable[index('e')] == 0 && hashTable[index('s')] == 0 && hashTable[index('t')] == 0)
break;
}
}
小结
- 哈希的内容是个数还是类别就可。
- 懂得抽象需要哈希的内容。大多数字符开256大小的数组就可以了。
今天刷题速度挺快(因为没有debug,连样例都没试就提交了,就是这么自信😄)的三题半小时就刷完了,中间接了通电话,所以就打算刷四题,整理一下。
(其实今天写IOR
的shell
脚本,debug
了好久一直报错,就连我暴力输出配置文件也报错,我真是无法了,还在想我怎么老是写bug
,没想到晚上刷题就这么顺。)