wang126179 @ 2022-10-12 20:21:56
#include<bits/stdc++.h> //万能库
using namespace std;
int main(){
int a,n;
char b[100];
cin>>a>>b;
n=strlen(b);
for(int i=0;i<n;i++){
b[i]+=(a%26);
if(b[i]>'z'){
b[i]-=26;
}
}
cout<<b;
return 0;
}
by LJKX @ 2022-10-12 20:35:23
循环里面改成这样
if(b[i]+a%26>'z') b[i]+=(a%26)-26;
else b[i]+=(a%26);
by 07kzs @ 2022-10-12 20:40:17
@wang126179
要先判断再加a
AC CODE
#include<bits/stdc++.h> //万能库
using namespace std;
int main(){
int a,n;
char b[100];
cin>>a>>b;
n=strlen(b);
for(int i=0;i<n;i++){
if(b[i]+(a%26)>'z'){
b[i]-=26;
}
b[i]+=(a%26);
}
cout<<b;
return 0;
}
by wang126179 @ 2022-10-13 07:13:20
@07kzs dalao能不能说一下区别,我感觉条件语句中两个都要加a%26,所以就直接加了,先加和之后加有什么不一样,谢谢了
by 07kzs @ 2022-10-13 19:45:19
@wang126179 因为当将字符转为ASCALL码时,z为122,最大的ASCALL码为127,所以先加会超出ASCALL码的范围,答案自然就会出问题。
by wang126179 @ 2022-10-14 15:39:58
@07kzs 懂了,谢谢解答