31. 素数

题目

问题描述

判断一个数是不是素数

输入说明

第一行为N

下面N行,每行一个数字n

输出说明

判断n是不是素数,是就输出yes,否则输出no

个人总结

思路

素数(又称质数)是指在大于1的自然数中,除了1和该数自身外,无法被其他自然数整除的数。例如,2、3、5、7、11等都是素数。

判断一个数n是否为素数,只需要尝试从2除到根号n即可。原理是如果n有因子,必然是一个小于等于根号n,另一个大于等于根号n,所以只要前半段没找到因子,后半段也不用找了,直接由flag变量判断输出。

这题最大的难点在各种边界条件。

易错点

  1. 循环终止条件必须包含根号n。比如判断9,根号9是3,如果只写小于3,就会漏掉因子3,导致把9误判为素数。
  2. 数字1单独处理。1循环进不去,flag保持默认真值,会误判为素数。必须在循环外加 if(n<=1) 特判输出no。
  3. 在C++中sqrt返回浮点数,直接在循环条件里写 j<=sqrt(n) 可能会因为精度问题或重复计算导致慢,建议用乘法代替,即 j*j<=n。

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int N;
	cin >> N;
	
	for (int i = 0; i < N; i++){
		int n;
		cin >> n;
		
		bool p = true;
		
		for (int j = 2; j * j <= n; j++){ // 注意根号n本身也要取到
			if (n % j == 0){
				p = false;
				break;
			}
		}
		
		if (n == 1){ // n==1 的情况前面的循环会直接跳过,需要单独处理
			p = false;
		}
		
		if (p){
			cout << "yes\n";
		} else {
			cout << "no\n";
		}
	}
	
	return 0;
}

32. 计算 e

题目

问题描述

利用公式e=1+ 1/1! + 1/2! + 1/3! + ... + 1/n!,编程计算e的近似值,直到最后一项的绝对值小于threshold(该项不包括在结果内),输出e的值并统计累加的项数。

输入说明

输入一个实数threshold,表示累加的阈值,数列中最后一项的值大于等于该阈值。Threshold最小可为1e-10。

输出说明

输出一个实数表示e的值,保留6位小数,并输出一个整数,表示累加的项数。两个数字之间用一个空格分隔,在行首和行尾没有多余的空格。

个人总结

  1. 核心思路是使用 while(true) 循环不断计算下一项的值。
  2. 每一轮循环中需要计算 1 除以 n 的阶乘,这里使用了内层 for 循环来现算阶乘。
  3. 题目要求小于阈值的那一项不包含在结果内,如果把判断作为循环条件,计算单项值和累加都在循环体内,会导致发现小于 threshhold 时已经把不符合条件的 a 加上去了。所以计算出当前项 a 后,必须立即判断 a < t,如果成立,在累加之前 break 跳出循环。
    若未达到阈值,则将 a 加到 e 上,并将表示项数的 n 加 1,准备计算下一轮。
  4. 每次进入计算阶乘的循环前,必须把变量 a 重置为 1,否则会保留上一轮的结果导致计算错误。
  5. 输出时 e 需要强制保留 6 位小数,使用 fixed 和 setprecision(6),项数直接输出 n 即可。虽然在计算过程中 n 作为分母阶乘项数是比实际项数少1的,但是在最后一次符合条件的循环中(也就是倒数第二次循环), n 在结尾依然会无条件+1 ,所以又与项数保持一致。

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	double t;
	cin >> t;
	
	double a = 1; // 单项值
	int n = 0; // 当前是几的阶乘,比项数少1
	double e = 0;
	
	while (true){
		
		// 算单项阶乘分数
		a = 1;
		for (int i = 1; i <= n; i++){
			a /= i;
		}
		if (a < t){ // 达到结束条件
			break;
		}
		e += a;
		n++;
	}
	
	cout << fixed << setprecision(6) << e;
	cout << setprecision(0) << " " << n << "\n";
	
	return 0;
}

 题目3

题目

问题描述

输入一个正整数,求这个正整数的各位数字之和。

输入说明

你的程序需要从标准输入设备(通常为键盘)中读入多组测试数据。每组测试数据为正整数,每行一个N,N小于20000。

输出说明

对每组测试数据,你的程序需要向标准输出文件(通常为启动该程序的文本终端)依次输出一组对应的答案:输出为它的各位数字之和,所有数据前后没有多余的空行,两组数据之间也没有多余的空行。

个人总结

  1. 通过 N % 10 取得当前最后一位数字累加,接着执行 N /= 10 去掉这一位。循环直到 N 变为 0 停止。

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int N;
	while (cin >> N){
		int sum = 0;
		int n;
		// 从后往前提取每一位
		while (N != 0){
			n = N % 10;
			sum += n;
			N /= 10;
		}
		
		cout << sum << "\n";
	}
		
	
	return 0;
}

计算机英语翻译练习

II. Research Methodologies

To appreciate the field of artificial intelligence, it is helpful to understand that it is being pursued along two paths. One is the engineering track in which researchers are trying to develop systems that exhibit intelligent behavior. The other is a theoretical track in which researchers are trying to develop a computational understanding of animal—especially human— intelligence. This dichotomy is clarified by considering the manner in which the two tracks are pursued. The engineering approach leads to a performance-oriented methodology because the underlying goal is to produce a product that meets certain performance goals. The theoretical approach leads to a simulation-oriented methodology because the underlying goal is to expand our understanding of intelligence and thus the emphasis is on the underlying process rather than the exterior performance.

要深入了解人工智能领域,理解该领域正沿着两条路径发展是大有裨益的。

一条是工程路径,研究人员致力于开发能够表现出智能行为的系统。

另一条是理论路径,研究人员试图建立对动物(尤其是人类)智能的计算化理解。

通过考察这两条路径的研究方式,可以更清晰地理解这种二分法

工程道路带来的是性能导向的方法论,因为其根本目标是开发出满足特定性能要求的产品。

理论道路带来的是模拟导向的方法论,因为它的重点目标是扩展我们对智能的认知,因此它的重点在于底层过程而不是外在表现。

As an example, consider the fields of natural language processing and linguistics. These fields are closely related and benefit from research in each other, yet the underlying goals are different. Linguists are interested in learning how humans process language and thus tend toward more theoretical pursuits. Researchers in the field of natural language processing are interested in developing machines that can manipulate natural language and therefore lean in the engineering direction. Thus, linguists operate in simulation-oriented mode—building systems whose goals are to test theories. In contrast, researchers in natural language processing operate in performance-oriented mode—building systems to perform tasks. Systems produced in this latter mode (such as document translators and systems by which machines respond to verbal commands) rely heavily on knowledge gained by linguists but often apply "shortcuts" that happen to work in the restricted environment of the particular system.

以自然语言处理和语言学为例,这两个领域联系紧密,相辅相成,但是底层目标却不同。

语言学家感兴趣的是了解人类如何处理语言,因此倾向于更偏向理论的追求。而自然语言处理领域的研究人员则致力于开发能够处理自然语言的机器,因此更偏向工程方向。

因此语言学家操作模拟导向模型,建立以测试理论为目标的系统。自然语言处理学家则恰恰相反,他们操作性能导向模型,建立任务执行系统。

使用后者模式制造的系统(例如文档翻译器或者是响应语音命令的机器)非常依赖语言学家提供的知识,但也经常加入特定系统在限制环境下工作恰好出现的“捷径”。

As an elementary example, consider the task of developing a shell 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 entire English language. More precisely, the shell does not need to distinguish between the various meanings of the word copy.(ls it noun or a verb? Should it carry the connotation of plagiarism?) Instead, the shell needs merely to distinguish the word copy from other commands such as rename and delete. Thus the shell could perform its task just by matching its inputs to predetermined audio patterns. The performance of such a system may be satisfactory to an engineer, but the way it is obtained would not be aesthetically pleasing to a theoretician.

还有一个基本的例子,那就是为一个通过英语语音命令来接收外部事件指令的操作系统开发 shell。

在这种情况下,shell (智能体)并不需要操心整个英语语言,更准确的说,它不需要分辨“复制”这个词的不同意思。(它是名词还是动词?是否暗含抄袭的含义?)

相反,它只需要把“复制”这个词和其他命令区分开来,比如说“重命名”或“删除”。因此, shell 只要把输入和预设的语音模式相匹配就可以完成工作。

这样一个系统的性能对于一位工程师来说已经足够令人满意了,但是它的实现方式对理论学家来说并不优雅。

附录

因为实在受不了每天整理翻译时 OCR 的换行问题,就 vibe 了一个格式处理工具,只有一个单文件,完全针对我自己的场景,估计其他人也用不上,懒得建项目仓库了,顺手在笔记里备份一下。以后有空可能会专门建个项目,完善成一个完整可用的工具吧(其实不太可能🤔)。

如果电脑上有好几个 python 环境,不想每次启动都要手动指定 python,可以建一个这个 bat ,指定 py 文件使用的 python,双击 bat 文件就能运行程序。改一改就能用了,不清楚或报错就去找 AI 问问。

@echo off
:: 前面是 python 路径,后面接脚本名
"C:\Users\username\anaconda3\envs\toolbox\pythonw.exe" "PassageMerge.py"

下面的简介是 AI 写的, AI 程序 + AI 简介,可谓原汤化原食。

PassageMerge - 剪贴板文本拼接与清洗工具

[ 简介 ] 这是一个基于 PySide6 开发的桌面悬浮小工具,主要用于解决 OCR 或从 PDF 中复制文本时出现的“硬换行”问题。它可以自动去除多余的换行符、合并断开的句子,并将处理后的文本自动写回剪贴板。

外观:

核心功能:

  1. 始终置顶的极简黑色窗口,不占用屏幕空间。
  2. 自动清洗:去除换行符 (\r\n),将多余空白压缩为单个空格。
  3. 两种模式:覆盖模式 (Clean) 和 追加模式 (Append)。

[ 环境依赖 ] 运行此脚本需要安装 PySide6: pip install PySide6

[ 启动方式 ] 直接运行 python 文件即可,或者另外写一个 bat 用来一键启动。启动后窗口默认位于屏幕右下角,支持鼠标拖拽移动。

[ 使用说明 ]

  1. 基本流程:

    • 第一步:在 PDF/网页 中选中并复制一段文本,或者是使用 OCR 工具获取文本。
    • 第二步:点击工具上的按钮(Clean 或 Append),此时处理好的文本已自动进入剪贴板。
    • 第三步:去翻译软件或笔记中粘贴(Ctrl+V)。
  2. 按钮功能详解:

    • Clean (清洗/覆盖): 当你开始复制一个新的段落时使用。它会读取剪贴板,清洗格式,并“清空”工具内部的旧缓存。

    • Append (拼接/追加): 当一个句子跨页或跨栏被切断时使用。 先复制前半段 -> 点击 Clean。 再复制后半段 -> 点击 Append。 工具会自动将后半段拼接到前半段后面(中间加空格),此时剪贴板里是完整的长句。

[ 界面状态说明 ]

  • 中间数字:显示当前缓存内的字符总数。
  • 左侧圆点颜色含义: ● 灰色:空闲/缓存为空。 ● 绿色:操作成功,文本已写入剪贴板。 ● 蓝色:缓存中有内容,提示你可以继续追加 (Append)。 ● 红色:剪贴板为空,读取失败。
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐