yuyuyuyu12345 @ 2020-03-05 20:02:26
#include <stdio.h>
#include <string.h>
int main(){
int n;
char b[100];
scanf("%d%s",&n,&b);
for(int i=0;i<strlen(b);i++){
b[i]+=n;
}
for(int j=0;j<strlen(b);j++){
printf("%c",b[j]);
}
return 0;
}
by ud2_ @ 2020-03-05 20:05:35
@yuyuyuyu12345
z
的下一个字母是a
,如此循环。
但是你的代码里并没有体现出这句话。
另外,输出可以用 puts(b)
。
by HearTheWindSing @ 2020-03-05 20:05:49
@yuyuyuyu12345 z的下一个是a呀,你这样的话z的下一个就是别的字符了鸭
by Lutalan @ 2020-03-05 20:06:07
如果输入数据是 1 zzz 的话 你这程序会输出{{{
by YUYGFGG @ 2020-03-05 20:07:07
加n后大于‘Z'
by sun_yh @ 2020-03-05 20:08:28
#include <stdio.h>
#include <string.h>
int main(){
int n;
char b[100];
scanf("%d%s",&n,b);
int len=strlen(b);
for(int i=0;i<len;i++){
int num=b[i]-'a';
num+=n;
num%=26;
b[i]='a'+num;
}
for(int j=0;j<len;j++){
printf("%c",b[j]);
}
return 0;
}
加n后要取模,不然会超26
by yuyuyuyu12345 @ 2020-03-05 20:09:10
@sjx233_ 谢谢,懂啦
by yuyuyuyu12345 @ 2020-03-05 20:09:18
@wangyxhaha 谢谢,懂啦
by yuyuyuyu12345 @ 2020-03-05 20:09:28
@造梦李 谢谢,懂啦
by sun_yh @ 2020-03-05 20:09:52
另外strlen()是O(size)的,写在循环里在某些题里时间复杂度会炸@yuyuyuyu12345
by yuyuyuyu12345 @ 2020-03-05 20:10:18
@sun_yh 懂啦,谢谢大佬的修改