40分,输出结果为什么是这样的?

P1914 小书童——凯撒密码

aouyou @ 2024-07-20 17:21:11

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    char a[51];
    cin >> a;
    char b[51];
    for (int i = 0; i < 51; i++)
    {
        if (!a[i])
            break;
        else
        {
            if ((a[i] + n) <= 122)
                b[i] = a[i] + n;
            else b[i] = a[i] + n - 26;
        }

    }
    cout << b << endl;
    return 0;
}

输入:\ 1\ abc\ 输出:\ bcd烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫?


by Clare613 @ 2024-07-20 17:35:28

你要在a.size()的范围内!


by Clare613 @ 2024-07-20 17:35:50

code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string a,b;
    cin >> a;
    for (int i = 0; i < a.size(); i++)
    {
        if ((a[i] + n) <= 122) b += char(int(a[i]) + n);
        else b += char(int(a[i]) + n - 26);
    }
    cout << b << endl;
    return 0;
}

by Clare613 @ 2024-07-20 17:36:01

求关注


|