OpenJudge NOI 1.7 32:行程长度编码

【题目链接】

OpenJudge NOI 1.7 32:行程长度编码

【题目考点】

1. 字符串

【解题思路】

本题与OpenJudge NOI 1.7 31:字符串p型编码基本相同。可以参考上一题的解析。(1.7 31解析)

【题解代码】

解法1:使用字符数组
#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[1005], c = '\0';//c:当前关注的字符 为大写字母 
    cin >> s;
    int len = strlen(s), num = 0;//num:连续出现的字符的个数 
    for(int i = 0; i <= len; ++i)
    {
        if(s[i] == c || s[i] - 32 == c)//若s[i]或s[i]变为大写后与字符c相同。s[i]若为小写字母,其对应的大写字母为s[i]-32 
            num++;
        else
        {
            if(c != '\0')//如果刚刚看到第0个字符,则不输出 
                cout << '(' << c << ',' << num << ')';
            if(s[i] >= 'a' && s[i] <= 'z')
                c = s[i] - 32;
            else
                c = s[i];
            num = 1;
        }
    }
    return 0;
}
解法2:使用string类及<cctype>函数
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    char c = '\0';//c:当前关注的字符 为大写字母
    cin >> s;
    int num = 0;//num:连续出现的字符的个数 
    for(int i = 0; i <= s.length(); ++i)
    {
        if(toupper(s[i]) == c)//若s[i]或s[i]变为大写后与字符c相同。 
            num++;
        else
        {
            if(c != '\0')//如果刚刚看到第0个字符,则不输出 
                cout << '(' << c << ',' << num << ')';
            c = islower(s[i]) ? toupper(s[i]) : s[i];
            num = 1;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值