wnsndg @ 2024-02-06 20:19:20
这道题看着我好像做出来了,但是只拿了60,为什么
请各位大佬指正=)
#include<iostream>
#include<string>
using namespace std;
string s;
int main(){
int n;
cin>>n>>s;
for(int i=0;i<s.size();i++){
s[i]=s[i]+n;
if(s[i]>'z')s[i]-=26;
}
cout<<s<<endl;
return 0;
}
by 2021zjhs005 @ 2024-02-06 20:21:03
@wnsndg 我个人认为 z
大多次。
解决方案:
if(s[i]>'z')
改为 while(s[i]>'z')
。或者:
by 2021zjhs005 @ 2024-02-06 20:23:02
char
的范围只有
by 2021zjhs005 @ 2024-02-06 20:24:33
为了防止越界,要不一个一个加上去?当然
by Zen_ @ 2024-02-06 20:27:50
#include<iostream>
#include<string>
using namespace std;
string s;
int main(){
int n;
cin>>n>>s;
for(int i=0;i<s.size();i++){
if(int(s[i])+n>int('z'))s[i]+=n-26;
else s[i]=s[i]+n;
}
cout<<s<<endl;
return 0;
}
by Zen_ @ 2024-02-06 20:28:19
@wnsndg 先判断,不然会越界
by wnsndg @ 2024-02-06 20:50:01
好滴,谢谢大佬
by wnsndg @ 2024-02-06 20:51:57
感谢大佬指正 我还会努力哒
by wnsndg @ 2024-02-06 21:03:12
谢谢,AC啦
by wnsndg @ 2024-02-06 21:04:08
@Zen_ @2021zjhs005 太谢谢了