Binary Protocol (Codeforces - 825A)

本文解析了CodeForces平台上A类题目BinaryProtocol的解决方法,介绍了如何将一种特殊的二进制字符串转换回其对应的十进制数值。通过示例说明了加密规则,并提供了清晰的代码实现。

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

题目链接  http://codeforces.com/problemset/problem/825/A

A. Binary Protocol
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has just invented a new binary protocol for data transmission. He is encoding positive integer decimal number to binary string using following algorithm:

  • Each digit is represented with number of '1' characters equal to the value of that digit (for 0 it is zero ones).
  • Digits are written one by one in order corresponding to number and separated by single '0' character.

Though Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number.

Input

The first line contains one integer number n (1 ≤ n ≤ 89) — length of the string s.

The second line contains string s — sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 109. The string always starts with '1'.

Output

Print the decoded number.

Examples
input
3
111
output
3
input
9
110011101
output
2031
题目大意:

第一行输入一个整数n,第二行输入一个长度为n的字符串。题目要求按照一定的规则转换成十进制。加密规则为:

如果遇到‘1’,那么输出‘1’的连续个数;例如:1111,就输出4;111就输出3;

如果遇到‘0’,那么就输出‘0’的里阿奴个数减1(因为数字是以0隔开的);例如:11011,就输出22;110011,就输出202;

解题思路:

这就是考察逻辑思维的,不需要多解释。希望读者跟着代码走一遍,就一定会明白了其中的道理。其实非常简单。

代码:

#include<iostream>
using namespace std;
int main()
{
    int n;
    char c[95];
    while(cin>>n)
    {
        int sum1=0;
        for(int i=0;i<=n-1;i++)
            cin>>c[i];
        for(int i=0;i<=n-1;i++)
        {
            if(c[i]=='1')
                sum1++;
            else
            {
                cout<<sum1;
                sum1=0;
            }
        }
        cout<<sum1<<endl;   //最终那一组数据需要最后单独输出,因为上面没有输最后一组数据
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值