01串压缩编码 [SCU – 1120] 题解

01串压缩编码 [SCU – 1120] 题解

简单的模拟,按题目做就行

#include <cstdio>
void putIntoMem(int x,char* memPos)
{
	int i;
	for(i=7;i>=0;i--)
	{
		memPos[i]= x&01 ;
		x>>=1;
	}
}
void NumToHex(int currVal,int count)
{
	int res=0;
	if(currVal==1)
	{
		res=0x80;
	}
	res|=(count&0x7F);
	printf("%d",res);
}
int main(void)
{
	char * memSlice,currVal,initFlag=true;
	int N,i,x,count;
	scanf("%d",&N);
	memSlice=new char [N];
	for(i=0;i<N/8;i++)
	{
		scanf("%d",&x);
		putIntoMem(x,memSlice+i*8);
	}
	#ifdef MALICVERBOSE
	for(i=0;i<N;i++)
		printf("%d",memSlice[i]);
	printf("\n");
	#endif
	currVal=memSlice[0];
	count=0;
	for(i=0;i<N;i++)
	{
		if(memSlice[i]==currVal)
			count+=1;
		else
		{
			if(initFlag==true)
			{
				initFlag=false;
			}
			else
			{
				printf(" ");
			}
			NumToHex(currVal,count);
			count=1;
			currVal=memSlice[i];
		}
	}
	if(initFlag==true)
	{
		initFlag=false;
	}
	else
	{
		printf(" ");
	}
	NumToHex(currVal,count);
	printf("\n");
	delete [] memSlice;
	return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注