我的也是3,4样例过不了,大佬看看吧

P1914 小书童——凯撒密码

elong123 @ 2024-04-06 21:20:51

//p1914凯撒密码
#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int n;
    cin >> n >> s;
    if(n == 0) cout << s;
    else
    for(int i =0; i < s.size();i++){
        if(s[i] >= 'a' && s[i] < 'z')
             s[i] =  s[i] + n;
        else
            s[i] = s[i] - 26 + n;   
    cout << s[i];
    }
    return 0;
}

by elong123 @ 2024-04-06 21:29:53

说错了,样例能过


by Nian_Gao @ 2024-04-06 21:30:48

#include<iostream>
#include<string>

using namespace std;

int n;
string s;
int main(){

    cin >> n >> s;
    for(int i = 0;i < s.size();i++){
        if(s[i] + n > 122){
            s[i] -= 26;
        }
        s[i] += n;
    }
    cout << s;
    return 0;
}

by malinhao45 @ 2024-04-25 20:47:46

因为你如果是s[i]==y,然后n=2,那么它就不会输出a,而是ascll码为z+1的字符。


by malinhao45 @ 2024-04-25 20:49:43

//p1914凯撒密码
#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int n;
    cin >> n >> s;
    if(n == 0) cout << s;
    else
    for(int i =0; i < s.size();i++){
        if(s[i]+n >= 'a' && s[i]+n <= 'z')
             s[i] =  s[i] + n;
        else
            s[i] = s[i] - 26 + n;   
    cout << s[i];
    }
    return 0;
}

by malinhao45 @ 2024-04-25 20:51:58

AC记录


|