60分求调

P1914 小书童——凯撒密码

Kingson999 @ 2024-06-22 10:40:50

#include<bits/stdc++.h>
using namespace std;
/*abcdefghijklmnopqrstuvwxyz*/
int n;
char a[114514];
int main(){
    cin>>n;
    cin>>a;
    int len=strlen(a);
    for(int i=0;i<len;i++){
        if(a[i]>='a'&&a[i]<=char('z'-n)){
            a[i]=char(a[i]+n);
        }
        else{
            a[i]=char((a[i]+n)%26+'a');
        }
    }
    cout<<a;
}

by 210101zhaosicheng @ 2024-06-22 11:29:19

@Kingson999 你下面的写错了,应该是:

a[i]=char((a[i]-'a'+n)%26+'a');

否则会超过26

全部的代码是:

#include<bits/stdc++.h>
using namespace std;
/*abcdefghijklmnopqrstuvwxyz*/
int n;
char a[114514];
int main(){
    cin>>n;
    cin>>a;
    int len=strlen(a);
    for(int i=0;i<len;i++){
        if(a[i]>='a'&&a[i]<=char('z'-n)){
            a[i]=char(a[i]+n);
        }
        else{
            a[i]=char((a[i]-97+n)%26+'a');//97等价于'a'
        }
    }
    cout<<a;
}

by 210101zhaosicheng @ 2024-06-22 11:29:40

@Kingson999


|