UVA11398 The Base-1 Number System【进制】

As we know, in an n-based number system, there are n different types of digits. In this way, a 1-based number system has only 1 type of digit, the ‘0’. Here are the rules to interpret 1-based numbers. Each number consists of some space separated blocks of 0. A block may have 1, 2 or more 0’s. There is a ‘flag’ variable associated with each number
• A block with a single ‘0’ sets ‘flag’ variable to 1
• A block with two 0’s sets the ‘flag’ to 0
• If there are n (n > 2) 0’s in a block, n−2 binary digits with the current value of flag is appended to your number.
    Note that, the first block of every number will have at most 2 0s. For example, the 1-base number ‘0 0000 00 000 0 0000’ is equivalent to binary ‘11011’.
• 1st block sets the flag to 1
• 2nd block has 4 0’s. So append flag(=1) 4 2 = 2 times (11).
• 3rd block has 2 0’s. Set the flag to 0
• 4th block has 3 0’s. Append flag(=0) 3-2 = 1 time (110).
• 5th block has a single ‘0’. Set flag = 1
• 6th and block has 4 0’s. Append flag(=0) 4-2=2 times (11011).
    The final binary number wont have more than 30 digits. Once, youve completed the process, convert the binary value to decimal and print, youre done!
Input
Input will have at most 100 test cases. Each case consists of a 1-based number as described above. A number may be spanned to multiple lines but a single block will always be in a single line. Termination of a case will be indicated by a single ‘#’ char which will be space-separated from the last digit of your input number. The last case in the input is followed by a ‘~’ character indicating, end of input.
Output
For each test case, output a single line with the decimal equivalent value of your given 1-based number.
Sample Input
0 0000 00 000 0 0000 #
0 000 #
~
Sample Output
27
1

问题链接UVA11398 The Base-1 Number System
问题简述:(略)
问题分析
    简单进制问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11398 The Base-1 Number System */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    while (cin >> s && s != "~") {
        string ans;
        char ch = '0';
        do {
            if(s == "#") break;
            else if(s.length() == 1) ch = '1';
            else if(s.length() == 2) ch = '0';
            else ans += string(s.length() - 2, ch);
        } while(cin >> s);

        int sum = 0;
        for(int i = 0; ans[i]; i++) {
            sum <<= 1;
            sum += ans[i] - '0';
        }

        printf("%d\n", sum);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值