算法竞赛入门经典(第二版)-刘汝佳-第四章 函数与递归 信息解码

http://acm.hust.edu.cn/vjudge/contest/123676#problem/D  密码5201


#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
//freopen("C:\\Users\\5201\\Desktop\\1.txt","r",stdin);
	char s[65535];
	while(gets(s))
	{
		char code[11][1<<8]={};
		int i,j,len=strlen(s),k=1;
		code[1][0]=s[0];
		for(i=2;k<len;i++)
		{
			int t=pow(2,i)-1;
			for(j=0;k<len&&j<t;j++,k++)
			{
				code[i][j]=s[k];
			}
		}
		while(1)
		{
			char ch;
			int a=0;
			for(i=0;i<3;i++)
			{
				do{ch=getchar();}while(ch=='\n');
				a=a*2+ch-'0';
			}
			int t=0;	
			if(a==0) goto end;		
			while(1)
			{
				t=0;
				for(i=0;i<a;i++)
				{
					do{ch=getchar();}while(ch=='\n');
					t=t*2+ch-'0';
				}
				if(t==pow(2,a)-1) break;
				printf("%c",code[a][t]);
			}
		}
	end:
		printf("\n");
		char ch;
		while(ch=getchar())
		{
			if(ch=='\n');
			else if(ch==EOF||ch=='\r') return 0;
			else 
			{
				ungetc(ch,stdin);
				break;
			}
		}
	}
	return 0;
}



ps  

1.题目数据  

.TNM AEIOU

0010101100011

1010001001110110011

11000

$#**\

0100000101101100011100101000


TAN ME

##*\$


2.循环读入 注意判断空格。

3.用文件读入的话最后一个是  '\r'    我也不知道为什么。

4.解码方式


4.先读入要数据串,根据要求转换编码。

5.再读入三个字符(除去回车)在转换成十进制a,表示下面要循环读入a个字符(除去回车),然后从code数组中输出相应字符。

6.注意a的值和循环读入a个字符后判断退出循环的条件。




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值