卑微求help @ 2019-11-05 18:50:03
int main () { int i,j,k,l; scanf("%d",&l); char a[50]; scanf("%s",a); for(int i=0;a[i]!='\0';i++) { if(l>26){l=l%26;} if(a[i]+l<'z') {a[i]=a[i]+l;} else { a[i]=a[i]+l-'z'+'a'-1; } }
printf("%s",a);
}
by Zenurik @ 2019-11-05 18:53:26
希更展?用MD
by 夏夜晴朗 @ 2019-11-05 19:06:06
1 #include<stdio.h>
2 int main () {
3 int i,j,k,l;
4 scanf("%d",&l);
5 char a[50];
6 scanf("%s",a);
7 for(int i=0;a[i]!='\0';i++)
8 {
9 if(l>26){l=l%26;}
10 if(a[i]+l<'z')
11 {a[i]=a[i]+l;}
12 else
13 { a[i]=a[i]+l-'z'+'a'-1; }
14 }
15 printf("%s",a);
16 }
在你的代码的第9行,a[i]+l应该<='z'
原因:当a[i]='z'时,可以,但是当a[i]='y'而l=1时,会出现一个错误的答案.
更改方案应该是把"a[i]+l<'z'"改为"a[i]+1<=z".
希望这能帮助到你!
by 夏夜晴朗 @ 2019-11-05 19:06:50
@夏夜晴朗 抱歉,应该是a[i]+l<='z';
by LevenKoko @ 2019-11-05 19:07:05
希望更丰富的展现?使用Markdown
by 卑微求help @ 2019-11-07 11:29:11
@夏夜晴朗 感谢!!!
by LYY111 @ 2020-02-16 15:13:54
第十三行是啥意思