48天笔试强训——第17天

54 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第29天,点击查看活动详情


选择题

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

编程题

杨辉三角的变形

这一题如果仔细的进行归纳总结,就会发现每一行出现第一次出现偶数的地方具有一定的规律,规律如下: 第1,2行没有偶数,为-1,从第3行开始,奇数行第一个偶数在第2个位置,偶数行的分成两种情况:1.偶数的二分之一是奇数,那么第一个偶数在第4个位置。2.偶数的二分之一是偶数,那么第一个偶数在第3个位置。

计算某字符出现次数

首先要把大小写转换一下(都要进行统一转换),然后依次遍历该字符出现的次数。

python代码

s=input().lower()
a=input().lower()
print(s.count(a))

c语言代码

#include <stdio.h>
#include <string.h>
int main()
{
	char str[1003] = { 0 };
	//gets函数获取一行数据,因为会连带最后的换行一起获取,所以不用担心遗留的换行对后续输入造成影响
	gets(str);
	char find;
	scanf("%c", &find);//捕捉要查找的字符
	int count[128] = { 0 };//用于记录每个字符出现次数
	int len = strlen(str);//获取字符串实际长度
	for (int i = 0; i < len; i++) {
		count[str[i]] += 1; //对应字符的位置计数+1
	} int res = count[find];
	if (find >= 'A' && find <= 'Z') {//如果是A-Z需要将a-z对应的计数加上
		res += count[find + 32];
	}
	else if (find >= 'a' && find <= 'z') {//如果是a-z需要将A-Z对应的计数加上
		res += count[find - 32];
	} printf("%d\n", res);
	return 0;
}