六十分原地升天。哪位大佬救我一把

P1914 小书童——凯撒密码

SbasdianJulian @ 2018-10-28 19:19:05

#include<bits/stdc++.h>
using namespace std;
int n;
string a;
int main(){
    cin>>n;
    cin>>a;
    for(int i=0;i<a.length();i++){
        a[i]+=n;
        if(a[i]+n>'z'&&n<=26)a[i]-=26;
        if(n>26)a[i]=(a[i]-'a'+n)%26+'a';   
    }
    cout<<a;
    return 0;
}

by LokiASG @ 2018-10-28 19:26:38

直接让它每次加的小于26即可

for(int i=0;i<a.length();i++){
        a[i]+=n%26;
        if(a[i]>'z')a[i]-=26;
    }

by Jerrylee @ 2018-10-29 21:12:12

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    int n,s,x;
    cin>>n;
    cin>>a;
    s=a.size();
    for(int i=0;i<=s-1;i++)
    {
        x=a[i]-'a';
        x=(x+n)%26;
        a[i]=x+'a';
        cout<<a[i];
    }
    return 0;
}

|