作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在大学的一个项目中工作,他们使用的问题类似于典型的 Cesar Cipher。它更像是一个功能程序,需要是最基本的。
程序将从用户那里收到一个从 65
到 90
的数字,例如当用户插入 65
时将显示 68
。将添加 3
数字,但当用户输入 90
时,将输入 67
。 90+3 ---->90,65,66,67
。这是一个从 65
到 90
的循环。
#include <stdio.h>
int cesar_encrypted(int x)
{
return (x+3);
}
void test_cesar_encrypted(void)
{
int x;
scanf("%d", &x);
int z = cesar_encrypted(x);
printf("%s\n", z);
}
int main(){
test_cesar_basic();
}
我做了这个示例代码,但我们只能更进一步,如果你给 90
他会给 93
而我想要 67
。
谁能帮我把它绕到 90 左右?
最佳答案
您可以使用模运算符,它给出除法的余数:
int cesar_encrypted(int x)
{
return (x - 65 + 3)%(91 - 65) + 65;
}
实现 Sulthan 的建议(见评论),它看起来像这样:
int cesar_encrypted(int x)
{
const int n_chars = 'Z' - 'A' + 1;
const int shift = 3;
return (x - 'A' + shift)%n_chars + 'A';
}
关于c - 在 C 中使用简单的 Cesar 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32736540/
我在大学的一个项目中工作,他们使用的问题类似于典型的 Cesar Cipher。它更像是一个功能程序,需要是最基本的。 程序将从用户那里收到一个从 65 到 90 的数字,例如当用户插入 65 时将显
我是一名优秀的程序员,十分优秀!