ACM书中的题目 j-10

本文解析了一道关于字符平移的算法题,要求输入字符串中的每个字母在字母表中的位置向后移动一位,并考虑边界情况。文章提供了完整的C++代码实现及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

J-10

题目:
J - 10
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit

Status
Description
You may have heard of the book ‘2001 - A Space Odyssey’ by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don’t tell you how the story ends, in case you want to read the book for yourself :-)

After the movie was released and became very popular, there was some discussion as to what the name ‘HAL’ actually meant. Some thought that it might be an abbreviation for ‘Heuristic ALgorithm’. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get … IBM.

Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.

Input
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.

Output
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing ‘Z’ by ‘A’.

Print a blank line after each test case.

Sample Input
2
HAL
SWERC

Sample Output
String #1
IBM

String #2
TXFSD

题目简述:
该题目要求将所输入的字母在字母表中的位置向后移动一格,再将其输出
解题思路:
使用字母的ASCⅡ码来实现要求,用字符串操作,注意s.size()该函数的使用
题目细节:
题目要求遇’Z’变’A’,并且注意题目中对输出后跟一空行的要求
代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string a;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        int x=a.size();
        cout<<"String #"<<i+1<<endl;
        for(int t=0;t<x;t++)
        {
            if(a[t]=='Z')
            cout<<'A';
            else
            cout<<(char)(a[t]+1);
        }
        cout<<endl<<endl;
    }
    return 0;
}

体会:
一开始没注意到输出换行的细节,PE了好几次,弄的很郁闷,最后才发现,以后读题要仔细一点:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值