10878 - Decode the tape

题意: 
解码输入的磁带内容, 输出正确的 ASCII 码字符.

思路:
|bobbb.bbo| 等价于以下二进制 01000001 (忽略 . 和 | , o 代表1, b 代表 0),
即逐行把磁带内容转成二进制, 再转成 10 进制, 再转成 char 进行输出即可. 

要点:
求 2 的 N 次方可用函数 pow(2, N);

题目:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=1819

代码:

# include <iostream>
# include <string>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
# include <cctype>
# include <math.h>	// pow
using namespace std;


// 解码一行, 从右往左, 每个 o 代表 1, 空格代表 0, 类似 二进制表达式
char decodeLine(const string& line)
{
	int j = 0;
	int i = line.size();

	int dec = 0; 								// 十进制
	while(--i){
		if(line[i] == '|' || line[i] == '.')	// 不考虑 | 和 .
			continue;

		if(line[i] == 'o'){
			dec += pow(2, j);
		}

		++j;
	}

	return dec;
}



int main(int argc, char const *argv[])
{
	#ifndef ONLINE_JUDGE
		freopen ("10878_i.txt", "r", stdin);  
		freopen ("10878_o.txt", "w", stdout); 
	#endif
		
	string line;
	getline(cin, line);		// 读入第一行 ___________

	while(!cin.eof()){
		getline(cin, line);

		if(line == "___________")
			break;

		cout << decodeLine(line);
	}

	return 0;
}

环境: C++ 4.5.3 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE

转载于:https://my.oschina.net/zenglingfan/blog/142553

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值