Freya_ @ 2024-03-07 14:51:23
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
int n1;
char a[50]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x'};
cin>>n1>>s;
for(int i=0;i<s.size();i++){
i+=n1;
cout<<a[i];
}
return 0;
}
by wuwendi123 @ 2024-03-07 15:10:10
两个问题 1:你输出a[i]时候,i有可能会大于等于26,这时候你的数组a中后面是没有的。。 2:是字符偏移n,不是索引i偏移n
by Freya_ @ 2024-03-07 16:58:42
@wuwendi123 没懂,我应该怎么修改QAQ
by wuwendi123 @ 2024-03-07 17:15:23
@Freya_ 可以这样去处理一下字符,首先s[i]-'a'是找出当前字符距离a的偏移量,然后加上n1,这个时候可以计算加密后距离'a'的偏移量,取余26就是防止溢出,然后余数为0到25,再加上'a'就可以了
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
int n1;
cin>>n1>>s;
for(int i=0;i<s.size();i++){
char c = (s[i]-'a'+n1) % 26 + 'a';
cout<<c;
}
return 0;
}
by Freya_ @ 2024-03-11 14:00:38
@wuwendi123 明白了,谢谢!