ZOJ Problem Set - 1009 Enigma

本文介绍二战中德国使用的Enigma密码机工作原理及其被破解的过程。解析三转子Enigma结构,阐述其加密机制,并通过编程实现解密过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZOJ Problem Set - 1009
Enigma

Time Limit: 10 Seconds                                     Memory Limit: 32768 KB                            

In World War II, Germany once used an electronic encryption machine called   Enigma, which played a decisive role in the initial victories of Nazi Germany.   It was proved to be one of the most reliable encryption systems in history.   However, it was the blind trust on the reliability of the machine that brought   about the doom of its user.

  The structure of a one-rotor Enigma is shown as follows (the Enigma has only   six keys):

The key element of the Enigma is the rotor, as shown in the second figure,   which uses electronic circuits to transform plaintext (input from keyboard)   into cryptograph (output on screen). When one key on the keyboard is pressed,   the corresponding cryptograph is shown on screen. Then the rotor will automatically   revolve a one-letter-step to a different position. The following figures illustrate   how the rotor works when letter "b" is pressed three successively   times:

When letter "b" is pressed for the first time, the signal goes through   the circuit and "A" is shown on screen. When the key is released,   the rotor revolves one-letter-step to a different position that changes all   the corresponding circuits so that each letter now has a different cryptograph.   When letter "b" is pressed for the second time, the corresponding   cryptograph is "C". So when letter "b" is pressed for the   third time, the cryptograph is "E" according to the principle specified   above.

  Now the following figure shows the structure of a two-rotor Enigma.

The difference is that when a key is released, the second rotor won't revolve   a step until the first one has finished one circle and returns to the original   position. This is also the same in the case of three-rotor Enigma. That is:   Only after the first rotor has finished one circle and return to the initial   status, the second rotor will revolve a step. And only after the second rotor   has finish one circle, the third rotor will revolve a step.

   However, how did the Allied Forces obtain the information encrypted by Enigma?   A person named Hans-Thilo Schimdt was very essential. He acted as a spy and   provided the initial status of the three rotors in each Enigma to the Allied   Forces once a month. The Allied Forces thus got everything they wanted by deciphering   the intercepted cryptograph using the information offered by the spy.

  Now, please design a program to obtain the plaintexts using the information   offered by the Allied Forces.


Input

  The input file contains several test cases representing several three-rotor   Enigmas. The last test case in the input file is followed by a line containing   a number 0.

  Each case begins with a line containing an integer m (1 <= m <= 26) which indicates   the number of sequential letters each rotor has. The first letter will always   be A. (for example, m = 6 tells each rotor has 6 keys from A to F). The following   three lines describe the initial status of the three rotors respectively. Each   of them contains a string consisting of m capital character. For instance, a   rotor with the initial status "BADFEC" indicates that the initial   encrypt mechanism is to convert "abcdef" to "BADFEC", that   is, original letter "a" corresponding to cryptograph letter "B",   "b" to "A", "c" to "D", "d"   to "F", "e" to "E" and "f" to "C".   The forth line of each case contains an integer n which tells the number of   cryptographs generated by the above Enigma. Then the following n lines are the   n cryptographs respectively, which consist of m capital characters each.


  Output


  For each test case, the output should consist of two parts. The first line is   the number of Enigma and a colon. The following lines are the plaintexts deciphered   from the corresponding cryptographs. Each plaintext should be printed in one   line. Note: The characters in the plaintext should be converted to the corresponding   lowercases before they are printed.

  Insert a blank line between test cases.


Sample Input

  6
  BADFEC
  ABCDEF
  ABCDEF
  1
  ACE
  0


Output for the Sample Input

  Enigma 1:
  bbb

 这题搞了我半天的时间,最后终于把思路给搞清楚了,首先是将初始值变成差值,规则如图:

滚动规则就是:第一个轮子:i%m 第二个轮子:(i/m)%m 第三个轮子:(i/(m*m))%m;原文到密文和密文到明文转动方向相反

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
	int rotate[3];
	int rotor[3][30];
	int m;
	int cas=0;
	while(cin>>m&&m!=0)
	{
		cas++;
		if(cas!=1)cout<<endl;
		cout<<"Enigma "<<cas<<":"<<endl;
		for(int i=0;i<3;i++)
		{
			string str;
			cin>>str;
		    for(int j=0;j<m;j++)
		    {
		    	rotor[i][str[j]-'A']=str[j]-('A'+j);
                    }
		}
	    int n;
	    cin>>n;
	    for(int i=0;i<n;i++)
	    {
	    	string cytext;
	    	cin>>cytext;
			int len=cytext.length();
			for(int j=0;j<len;j++)
			{
			  rotate[0]=j%m;
			  rotate[1]=(j/m)%m;
			  rotate[2]=(j/(m*m))%m;
			  int index=cytext[j]-'A';
			  index=(index-rotor[2][(index-rotate[2]+m)%m]+m)%m;
			  index=(index-rotor[1][(index-rotate[1]+m)%m]+m)%m;
			  index=(index-rotor[0][(index-rotate[0]+m)%m]+m)%m;
			  cout<<(char)('a'+index);
			}
			cout<<endl;
	    }
	} 
}


标题“51单片机通过MPU6050-DMP获取姿态角例程”解析 “51单片机通过MPU6050-DMP获取姿态角例程”是一个基于51系列单片机(一种常见的8位微控制器)的程序示例,用于读取MPU6050传感器的数据,并通过其内置的数字运动处理器(DMP)计算设备的姿态角(如倾斜角度、旋转角度等)。MPU6050是一款集成三轴加速度计和三轴陀螺仪的六自由度传感器,广泛应用于运动控制和姿态检测领域。该例程利用MPU6050的DMP功能,由DMP处理复杂的运动学算法,例如姿态融合,将加速度计和陀螺仪的数据进行整合,从而提供稳定且实时的姿态估计,减轻主控MCU的计算负担。最终,姿态角数据通过LCD1602显示屏以字符形式可视化展示,为用户提供直观的反馈。 从标签“51单片机 6050”可知,该项目主要涉及51单片机和MPU6050传感器这两个关键硬件组件。51单片机基于8051内核,因编程简单、成本低而被广泛应用;MPU6050作为惯性测量单元(IMU),可测量设备的线性和角速度。文件名“51-DMP-NET”可能表示这是一个与51单片机及DMP相关的网络资源或代码库,其中可能包含C语言等适合51单片机的编程语言的源代码、配置文件、用户手册、示例程序,以及可能的调试工具或IDE项目文件。 实现该项目需以下步骤:首先是硬件连接,将51单片机与MPU6050通过I2C接口正确连接,同时将LCD1602连接到51单片机的串行数据线和控制线上;接着是初始化设置,配置51单片机的I/O端口,初始化I2C通信协议,设置MPU6050的工作模式和数据输出速率;然后是DMP配置,启用MPU6050的DMP功能,加载预编译的DMP固件,并设置DMP输出数据的中断;之后是数据读取,通过中断服务程序从DMP接收姿态角数据,数据通常以四元数或欧拉角形式呈现;再接着是数据显示,将姿态角数据转换为可读的度数格
MathorCup高校数学建模挑战赛是一项旨在提升学生数学应用、创新和团队协作能力的年度竞赛。参赛团队需在规定时间内解决实际问题,运用数学建模方法进行分析并提出解决方案。2021年第十一届比赛的D题就是一个典型例子。 MATLAB是解决这类问题的常用工具。它是一款强大的数值计算和编程软件,广泛应用于数学建模、数据分析和科学计算。MATLAB拥有丰富的函数库,涵盖线性代数、统计分析、优化算法、信号处理等多种数学操作,方便参赛者构建模型和实现算法。 在提供的文件列表中,有几个关键文件: d题论文(1).docx:这可能是参赛队伍对D题的解答报告,详细记录了他们对问题的理解、建模过程、求解方法和结果分析。 D_1.m、ratio.m、importfile.m、Untitled.m、changf.m、pailiezuhe.m、huitu.m:这些是MATLAB源代码文件,每个文件可能对应一个特定的计算步骤或功能。例如: D_1.m 可能是主要的建模代码; ratio.m 可能用于计算某种比例或比率; importfile.m 可能用于导入数据; Untitled.m 可能是未命名的脚本,包含临时或测试代码; changf.m 可能涉及函数变换; pailiezuhe.m 可能与矩阵的排列组合相关; huitu.m 可能用于绘制回路图或流程图。 matlab111.mat:这是一个MATLAB数据文件,存储了变量或矩阵等数据,可能用于后续计算或分析。 D-date.mat:这个文件可能包含与D题相关的特定日期数据,或是模拟过程中用到的时间序列数据。 从这些文件可以推测,参赛队伍可能利用MATLAB完成了数据预处理、模型构建、数值模拟和结果可视化等一系列工作。然而,具体的建模细节和解决方案需要查看解压后的文件内容才能深入了解。 在数学建模过程中,团队需深入理解问题本质,选择合适的数学模
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值