1月29日上机
题目:判断一个数是不是素数输入说明:第一行为N,下面N行,每行一个数字n输出说明:判断n是不是素数,是就输出yes,否则输出no示例:38972397输出:noyesyes个人总结:思路:(1)判断素数就是对一个数N进行循环(从2开始到N-1)结束,并判断是否有数使其的余数为0,若有数则证明该数不是素数,则输出no,反之则为yes。代码:int main()int N;i < N;// 默认认为是
一、判断是否为素数
题目:判断一个数是不是素数
输入说明:第一行为N,下面N行,每行一个数字n
输出说明:判断n是不是素数,是就输出yes,否则输出no
示例:
3
897
23
97
输出:
no
yes
yes
个人总结:
思路:(1)判断素数就是对一个数N进行循环(从2开始到N-1)结束,并判断是否有数使其的余数为0,若有数则证明该数不是素数,则输出no,反之则为yes。
代码:
#include <stdio.h>
int main()
{
int N;
scanf_s("%d", &N);
for (int i = 0; i < N; i++)
{
int num;
scanf_s("%d", &num);
// 默认认为是素数
int isPrime = 1;
if (num <= 1)
{
isPrime = 0;
}
else
{
for (int j = 2; j * j <= num; j++)
{
if (num % j == 0)
{
isPrime = 0; // 找到因子,不是素数
break;
}
}
}
if (isPrime)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
二、计算e
题目:利用公式e=1+ 1/1! + 1/2! + 1/3! + ... + 1/n!,编程计算e的近似值,直到最后一项的绝对值小于threshold(该项不包括在结果内),输出e的值并统计累加的项数。
输入说明:输入一个实数threshold,表示累加的阈值,数列中最后一项的值大于等于该阈值。Threshold最小可为1e-10。
输出说明:输出一个实数表示e的值,保留6位小数,并输出一个整数,表示累加的项数。两个数字之间用一个空格分隔,在行首和行尾没有多余的空格。
示例:输入:0.00001 输出:2.718279 9
个人总结:
易错点:
1.默认先加 1 导致结果错误
很多实现会直接将 e 初始化为 1,但当 threshold ≥ 1 时,第一项本应被舍弃,提前累加会导致结果与标准答案不符。
2.整数除法错误
若使用 int 类型计算 1 / fact,会发生整数除法,结果为 0,导致计算完全错误,必须使用 double。
3.判断顺序错误
若先累加再判断是否小于 threshold,会错误地把不应计入的项加入结果。
4.项数统计不准确count 应只统计实际被累加的项数,而不是循环次数或阶乘的次数。
代码:
#include<stdio.h>
#include<math.h>
int main()
{
double threshold;
double e = 0.0;
double fact = 1.0;
double term;
int n = 0;
int count = 0;
scanf_s("%lf", &threshold);
while (1)
{
if (n > 0)
{
fact = fact*n;
}
term = 1.0 / fact;
if (term < threshold)
{
break;
}
e = e + term;
count++;
n++;
}
printf("%.6f %d", e, count);
return 0;
}
三、数字之和
题目:输入一个正整数,求这个正整数的各位数字之和。
输入说明:你的程序需要从标准输入设备(通常为键盘)中读入多组测试数据。每组测试数据为正整数,每行一个N,N小于20000。
输出说明:对每组测试数据,你的程序需要向标准输出文件(通常为启动该程序的文本终端)依次输出一组对应的答案:输出为它的各位数字之和,所有数据前后没有多余的空行,两组数据之间也没有多余的空行。
示例:
输入:
10
99
输出:
1
18
个人总结:
本题较为简单,只需要各个数字之和则只需要对原数进行取余在相加,就可以完成该功能。
代码:
#include<stdio.h>
#include<math.h>
int main()
{
int num;
while (scanf_s("%d", &num) == 1)
{ int sum = 0;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
}
printf("%d\n", sum);
}
return 0;
}
四、英语翻译
1.To appreciate the field of artificial intelligence, it is helpful tounderstand that it is being pursued along two paths. One is theengineering track in which researchers are trying to developsystems that exhibit intelligent behavior. The other is atheoretical track in which researchers are trying to develop acomputational understanding of animalespecially humanintelligence. This dichotomy is clarified by considering themanner in which the two tracks are pursued. The engineeringapproach leads to a performance-oriented methodologybecause the underlying goal is to produce a product that meetscertain performance goals. The theoretical approach leads to asimulation-oriented methodology because the underlying goalis to expand our understanding of intelligence and thus theemphasis is on the underlying process rather than the exteriorperformance.
要理解人工智能这一领域,认识到它沿着两条路径发展是很有帮助的。一条是工程路径,研究人员试图开发能够表现出智能行为的系统;另一条是理论路径,研究人员试图建立对动物(尤其是人类)智能的计算性理解。通过考察这两条路径的研究方式,这种二分法会更加清晰。工程路径导向一种以性能为中心的方法论,因为其根本目标是产出满足特定性能指标的产品;而理论路径则导向一种以仿真为中心的方法论,因为其根本目标是扩展我们对智能的理解,因此关注的重点在于内部的智能过程,而非外在的性能表现。
2.As an example, consider the fields of natural language processing and linguistics. These fields are closely related andbenefit from research in each other, yet the underlying goals aredifferent. Linguists are interested in leaming how humansprocess language and thus tend toward more theoreticalpursuits. Researchers in the field of natural language processingare interested in developing machines that can manipulatenatural language and therefore lean in the engineering direction.Thus, linguists operate in simulation-oriented modebuildingsystems whose goals are to test theories. In contrast, researchersin natural language processing operate in performance-orientedmodebuilding systems to perform tasks. Systems produced inthis latter mode (such as document translators and systems bywhich machines respond to verbal commands) rely heavily onknowledge gained by linguists but often apply "shortcuts" thathappen to work in the restricted environment of the particularsystem.
例如,可以考察自然语言处理与语言学这两个领域。它们彼此关系密切,相互借鉴研究成果,但其根本目标却并不相同。语言学家关注的是人类如何处理语言,因此更倾向于从事理论性研究;而自然语言处理领域的研究人员则致力于开发能够操纵和处理自然语言的机器,因此更偏向工程方向。由此,语言学家通常采用以仿真为导向的研究模式,构建系统的目的在于检验理论;相对而言,自然语言处理研究人员则采用以性能为导向的研究模式,构建系统来完成具体任务。后一种模式下产生的系统(如文档翻译系统以及机器响应口头指令的系统)在很大程度上依赖语言学家所获得的知识,但往往会采用一些在特定、受限环境中“恰好有效”的捷径式方法。
3.As an elementary example, consider the task of developing ashell for an operating system that receives instructions from the outside world through verbal English commands. In this case,the shell (an agent) does not need to worry about the entireEnglish language. More precisely, the shell does not need todistinguish between the various meanings of the word copy.(Is ita noun or a verb? Should it carry the connotation ofplagiarism?) Instead, the shell needs merely to distinguish theword copy from other commands such as rename and delete.Thus the shell could perform its task just by matching its inputsto predetermined audio patterns. The perfommance of such asystem may be satisfactory to an engineer, but the way it isobtained would not be aesthetically pleasing to a theoretician.
作为一个初步的例子,可以考虑这样一项任务:开发一个操作系统的外壳程序(shell),它通过外界输入的英语口头指令来接收命令。在这种情况下,该外壳程序(即一个智能体)并不需要处理整个英语语言。更准确地说,它不必区分 copy 这个词的各种含义(它是名词还是动词?是否带有“抄袭”的含义?)。相反,它只需要将 copy 与诸如 rename、delete 等其他命令区分开即可。因此,这样的外壳程序仅通过将输入与预先设定的语音模式进行匹配,就可以完成其任务。对于工程人员而言,这种系统的性能可能是令人满意的;但对于理论研究者来说,其实现方式在美学上并不令人满意。
更多推荐



所有评论(0)