一、手机短号
题目:大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号。假设所有的短号都是"6"+手机号的后5位,比如号码为13512345678的手机,对应的短号就是645678。现在,如果给你一个11位长的手机号码,你能找出对应的短号吗?
输入说明:输入数据的第一行是一个N(N <= 1000),表示有N个数据,接下来的N行每一行为一个11位的手机号码。
输出说明:输出应包括N行,每行包括一个对应的短号,输出应与输入的顺序一致。
个人总结:
这道题本质上是一个字符串处理问题,使用到的核心算法思想非常简单,可以归纳为"字符串截取与拼接"。题目并不需要进行数值运算,也不涉及复杂的数据结构,而是利用字符数组按下标访问指定位置的字符,再按规则重新输出。
从算法角度来看,整体思路属于顺序结构 + 循环结构的线性处理。程序首先读入整数 N,然后通过一个 for 循环逐条读取手机号码。每个手机号是 11 位数字,但在程序中用字符数组 char phone[20] 存储,而不是用整型变量。这是一个关键点,因为手机号可能以 0 开头(虽然实际手机号不会),更重要的是它不需要做数值计算,仅作为字符串处理,用字符数组更安全、更合适。
代码:
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int N;
char phone[20];
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%s", phone);
printf("6");
for (int j = 6; j < 11; j++)
{
printf("%c", phone[j]);
}
printf("\n");
}
return 0;
}
二、字符串统计
题目:对于给定的一个字符串,统计其中小写字母出现的次数。
输入说明:输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
输出说明:对于每个测试实例,输出该串中数值的个数,每个输出占一行。
个人总结:
这道题本质上是一个字符串遍历与字符分类统计问题,核心算法思想是线性扫描(顺序遍历)加条件判断计数,属于典型的字符数组处理问题。整体时间复杂度为 O(n × m),其中 n 是测试用例个数,m 是每个字符串的长度,因为程序对每个字符串都逐字符扫描一次。
程序的主要算法过程可以概括为:先读入测试实例个数 n,然后通过循环逐行读取字符串;对于每一个字符串,从第一个字符开始遍历,直到遇到字符串结束符 '\0' 为止,在遍历过程中通过判断字符是否位于 'a' 到 'z' 之间来统计小写字母个数。如果满足条件则计数器加一,最终输出计数结果。这实际上就是一种最基础的"逐字符判定统计"算法,不涉及复杂数据结构或高级算法思想。
代码:
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int n;
scanf_s("%d", &n);
getchar();
char str[1000];
for (int i = 0; i < n; i++)
{
fgets(str, sizeof(str), stdin);
int count = 0;
for (int j = 0; str[j] != '\0'; j++)
{
if (str[j] >= 'a'&&str[j] <= 'z')
{
count++;
}
}
printf("%d\n", count);
}
return 0;
}
三、弟弟的作业
题目:你的弟弟刚做完了"100以内数的加减法"这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。
输入说明:输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
输出说明:输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
个人总结:
这道题本质上是一个字符串格式解析 + 条件判断统计的问题,核心算法思想属于线性读取、逐条判断、累计统计,整体时间复杂度为 O(n),其中 n 为题目行数(最多 100 行),每一行只处理一次。
程序采用 while (scanf(...) != EOF) 的方式读取输入,这是一种典型的"读到文件结束为止"的处理模式,适用于未知行数的输入场景。算法流程可以概括为:循环读取一行表达式 → 解析出 a、运算符 op、b 和结果 c → 判断是否为问号 → 若不是问号则计算正确结果 → 将字符串形式的答案转换为整数 → 比较是否正确 → 累加计数。
代码:
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main()
{
int a, b;
char op;
char c[20];
int count = 0; // 统计答对数量
while (scanf("%d%c%d=%s", &a, &op, &b, c) != EOF)
{
// 如果是问号,跳过
if (c[0] == '?')
continue;
int result;
if (op == '+')
result = a + b;
else
result = a - b;
int answer;
sscanf(c, "%d", &answer);
if (answer == result)
count++;
}
printf("%d\n", count);
return 0;
}
四、英语翻译
1.A computer willsolve problems in exactly the way it isprogrammed to, without regard to efficiency, alternativesolutions, possible shortcuts, or possible errors in the code.Computer programs that leamn and adapt are part of theemerging field of artificial intelligence and machine learning.Artificial intelligence-based products generally fall into twomajor categories: rule-based systems and pattern recognitionsystems. Rule-based systems attempt to represent the rules used by human experts and tend to be expensive to develop. Pattern-based systems use data about a problem to generateconclusions. Examples of pattern-based systems include voicerecognition, font recognition, translation and the emerging fieldof on-line marketing.
计算机将完全按照它被编程的方式来解决问题,而不会考虑效率、替代方案、可能的捷径或代码中可能存在的错误。能够学习和适应的计算机程序属于新兴的人工智能和机器学习领域。基于人工智能的产品通常分为两大类:基于规则的系统和模式识别系统。基于规则的系统试图表示人类专家所使用的规则,开发成本通常较高。基于模式的系统则利用与问题相关的数据来生成结论。模式系统的例子包括语音识别、字体识别、翻译以及新兴的在线营销领域。
2.Computer science is the study of the theory, experimentation,and engineering that form the basis for the design and use ofcomputers-devices that automatically process information.Computer science traces its roots to work done by Englishmathematician Charles Babbage, who firstproposed aprogrammable mechanical calculator in 1837. Until the advent of electronic digital computers in the 1940s, computer science wasnot generally distinguished as being separate from mathematicsand engineering. Since then it has sprouted numerous branchesof research that are unique to the discipline.
计算机科学是研究构成计算机设计与使用基础的理论、实验和工程学科。计算机是一种能够自动处理信息的设备。计算机科学可以追溯到英国数学家查尔斯·巴贝奇的工作,他在1837年首次提出了可编程机械计算器的设想。直到20世纪40年代电子数字计算机出现之前,计算机科学通常并未被认为是区别于数学和工程学的独立学科。此后,它逐渐发展出许多该领域特有的研究分支。
3.Early work in the field of computer science during the late 1940sand early 1950s focused on automating the process of makingcalculations for use in science and engineering. Scientists andengineers developed theoretical models of computation thatenabled them to analyze how efficient different approaches wereinperformingvariouscalculations.Computerscienceoverlapped considerably during this time with the branch ofmathematics known as numerical analysis, which examines theaccuracy and precision of calculations.
20世纪40年代末至50年代初,计算机科学领域的早期研究主要集中在自动化计算过程,以服务于科学和工程应用。科学家和工程师建立了计算的理论模型,使他们能够分析不同方法在执行各种计算时的效率。在这一时期,计算机科学与数学中的数值分析分支有着相当大的重叠。数值分析主要研究计算结果的准确性和精确性。
五、单词打卡
