ZOJ 1240 IBM Minus One

本文提供了解决ZJU ACM 240题目的两种方法,一种使用字符数组处理字符串,另一种使用标准字符串类。两种方法都实现了将输入字符串中的每个字符向后移动一位的功能,并按要求格式输出。

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=240

注意事项:题目要求 Print a blank line after each test case.

                   即无论是否是最后一行都要输出一个空行。

代码如下:

#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<queue>
#include<set>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
using namespace std;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("D:\\in.txt", "r", stdin);
        freopen("D:\\out.txt", "w", stdout);
    #endif
        char buf[51];
        int n;
        cin >> n;
        cin.get();
        for (int j = 0; j < n;j++)
        {
            cin.getline(buf, 51);
            if ('#' == buf[0])
            {
                break;
            }
            for (int i = 0; buf[i] != '\0'; i++)
            {
                if (buf[i] != 'Z')
                {
                    buf[i] = buf[i] + 1;
                }
                else
                {
                    buf[i] = 'A';
                }
            }
            cout << "String #" << j+1 << endl;
            cout << buf << endl;
            cout << endl;
        }
        return 0;
}

下面是使用String的做法:

#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<queue>
#include<set>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
using namespace std;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("D:\\in.txt", "r", stdin);
        freopen("D:\\out.txt", "w", stdout);
    #endif
        char buf[51];
        int n;
        string str;
        cin >> n;
        for (int j = 0; j < n;j++)
        {
            cin >> str;
            cout << "String #" << j + 1 << endl;
            for (int i = 0; i < str.size(); i++)
            {
                cout << (str[i]!='Z'?(char)(str[i]+1):'Z')<<endl;
                cout << endl;
            }

        }
        return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值