C38-解密宝箱
2025-01-11 19:08:47
发布于:江苏
7阅读
0回复
0点赞
一、上周作业回顾
Hw01. A22299.下一个字母
常规简单题,注意类型的转换
#include<iostream>
using namespace std;
int main(){
string a;
cin>>a;
for(int i=0;i<a.size();i++){
if(a[i]>='0'&&a[i]<='9'){
cout<<a[i];
}
else if(a[i]=='z'){
cout<<'a';
}
else if(a[i]=='Z'){
cout<<'A';
}
else{
cout<<char(a[i]+1);
}
}
}
Hw02. A29344.50%AI, 50%Human
注意整数的除法自动向下取整以及输出格式。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a=0, b=0;
string s;
cin >> n;
for (int i=1; i<=n; i++){
cin >> s;
if (s.size() > 5) a++;
}
int x = ceil(a*1.0/n*100);
int y = 100-x;
cout<<x<<"%AI, "<<y<<"%Human";
return 0;
}
二、课堂案例
/*
测试输入代码:
.---- -.- --.- -.... .-.. ----- -.. .--- --- ....
*/
#include <bits/stdc++.h>
using namespace std;
string morse[37] = {".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--","--..",
"-----",".----","..--","...--","....-",
".....","-....","--...","---..","----."
}; //存放摩斯密码的字符串数组
string a[11]; //待解密的密文
char b[11]; //1.摩斯密码的解密结果
char c[11]; //2.凯撒解密的结果
char d[3][6]; //3.栅栏解密的结果
int idx;
int main()
{
for (int i=0; i<10; ++i) cin>>a[i]; //输入密文
//1.摩尔斯解密
//先 拿出 a , 将a里面的每一个与morse[j]进行比较
for (int i=0; i<10; i++) {
for (int j=0; j<36; j++){
if (a[i] == morse[j]){
//morse[j]就是我要找的内容
if (j<26) b[idx++] = char(j+97);
else b[idx++] = char(j+22); //26+22 = 48
break;
}
}
}
cout<< b << endl;
//2.凯撒解密
for (int i=0; i<10; i++){
if (b[i]>='a' && b[i] <='c'){
c[i] = b[i] + 23;
}
else if (b[i]>='d' && b[i]<='z'){
c[i] = b[i] - 3;
}
else{
c[i] = b[i];
}
}
cout<< c << endl;
//3.栅栏解密
idx = 0;
for (int i=0; i<2; i++){
for (int j=0; j<5; j++){
d[i][j] = c[idx++];
}
}
//输出栅栏解密结果
for ( int i=0; i<5; i++){
for (int j=0; j<2; j++){
cout<<d[j][i];
}
}
cout << endl;
//4.杨辉三角
int e[25][25]= {0}, n=10;
for(int i=1; i<=n; i++) {
e[i][1]=1;
e[i][i]=1;
}
for(int i=3; i<=n; i++) {
for(int j=2; j<=i; j++) {
e[i][j]=e[i-1][j]+e[i-1][j-1];
}
}
cout << e[10][6] << endl;
return 0;
}
这里空空如也
有帮助,赞一个