60分,3·4节点错的稀里糊涂,求助,赏关注

P1914 小书童——凯撒密码

wangruhao @ 2024-03-31 21:36:17

#include <iostream>
#include <cstring>
using namespace std;
char a[55];
int main(){
    int n;
    cin>>n;
    cin >> a+1;
    int x;
    int la = strlen(a+1);
    for(int i =1;i<=la;i++){
        x=a[i]+n;
        if(x>122)
        {
            cout<<char(x-26);
        }
        else
        {
            cout<<char(x);
        }

    }
    return 0;
}

查ascii码是对的


by Fish_redflying @ 2024-03-31 22:06:44

因为Ta是往后移动,难免会出现越界的情况,所以要用取模

Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int ii;cin>>ii;
    char arr[100];
    scanf("%s",arr);
    for(int i=0;i<strlen(arr);i++) {
        arr[i]=97+(arr[i]+ii-97)%26;
    }
    printf("%s",arr);
}

by Fish_redflying @ 2024-03-31 22:06:59

@wangruhao


by xhl_leo @ 2024-04-03 16:39:08


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

by wangruhao @ 2024-04-03 20:25:26

谢谢,已关注


|