onlyTiming @ 2024-09-15 16:18:28
#include <stdio.h>
#include <string.h>
int main() {
char secret[50];
int n;
scanf("%d\n",&n);
scanf("%s", &secret);
for (int i = 0; i < 50; i++) {
if (secret[i] + n > 'z') {
int move = (secret[i] + n -'z') % 26;
secret[i] = 'a' + move -1;
}
else if(secret[i]>='a' && secret[i]<='z') {
secret[i] += n;
}
}
printf("%s",secret);
return 0;
}```
by kejun709394 @ 2024-09-15 17:11:59
#include <stdio.h>
#include <string.h>
int main() {
char secret[50];
int n;
scanf("%d\n",&n);
scanf("%s", &secret);
for (int i = 0; i < 50; i++) {
if (secret[i] + n > 'z') {
int move = (secret[i] + n -'z') % 26;
if(secret[i]=='z' && n==26)
secret[i] = 'z';
else
secret[i] = 'a' + move -1;
}
else if(secret[i]>='a' && secret[i]<='z') {
secret[i] += n;
}
}
printf("%s",secret);
return 0;
}
if (secret[i] + n > 'z') {
int move = (secret[i] + n -'z') % 26;
if(secret[i]=='z' && n==26)
secret[i] = 'z';
else
secret[i] = 'a' + move -1;
}
@onlyTiming 这样特判下就行了,你没有试过
26
z
这个测试样例吧吧?
给你看看我之前写的
#include<iostream>
using namespace std;
int main(){
int n;
string s;
cin>>n;
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]+n>'z'){
s[i]=s[i]+n-26;
}else{
s[i]=s[i]+n;
}
cout<<s[i];
}
return 0;
}
我是第一次写评论,勿喷
by onlyTiming @ 2024-09-15 17:59:04
@kejun709394 原来是这样,感谢!
by Asher0115 @ 2024-09-16 19:41:14
也可以这样
#include<bits/stdc++.h>
using namespace std;
int n;
string s,t="abcdefghijklmnopqrstuvwxyz";
int main()
{
t=t+t+t+t;
cin>>n>>s;
for(int i=0;i<s.size();i++)
{
int r;
for(int j=0;j<=25;j++)
{
if(s[i]==t[j])
{
r=j;
goto gg;
}
}
gg:;
cout<<t[r+n];
}
return 0;
}
如果没有“1≤n≤26。”,n又太大,t多加几次t就好了