tlbcsky @ 2023-12-22 22:24:28
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
cout<<char(s[i]+n);
}
return 0;
}
不知道有什么问题,求解
by xiao_dong_xi @ 2023-12-22 22:27:11
少了特判。想一想,如果输入有 z
怎么办?
by xiao_dong_xi @ 2023-12-22 22:27:58
@tlbcsky
by tlbcsky @ 2023-12-22 22:28:45
@JoneSno 噢噢噢噢懂了
by tlbcsky @ 2023-12-22 22:39:06
@JoneSno ```
using namespace std; int main() { int n; cin>>n; string s; cin>>s; for(int i=0;i<s.size();i++) { if(s[i]=='z') { cout<<char(s[i]-26+n); } else cout<<char(s[i]+n); } return 0; }
这下好了只有四十分了
by xiao_dong_xi @ 2023-12-22 22:40:39
不是啊,不仅仅是 z
,还有其他可能。比如 n=27,s[i]=a
by xiao_dong_xi @ 2023-12-22 22:43:38
@tlbcsky
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
s[i]=(s[i]+n-'a')%26+'a';
cout<<s[i];
}
return 0;
}
可以看懂吗?
by tlbcsky @ 2023-12-23 15:22:04
@JoneSno 勉强吧,不是特别能看懂
by xiao_dong_xi @ 2023-12-23 15:53:26
或者你可以这样
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
for(int j=1;j<=n;j++)//循环n次,每次右移一位,并判断是否大于z。
{
s[i]+=1;
if(s[i]>'z')s[i]='a';
}
//s[i]=(s[i]+n-'a')%26+'a';
cout<<s[i];
}
return 0;
}
by xiao_dong_xi @ 2023-12-23 15:53:35
@tlbcsky
by tlbcsky @ 2023-12-23 18:53:32
@JoneSno 抱歉啊才看到