Bitmap

描述:

Bitmap is a compression technology for redundant data compression. The ratio of Bitmap Compression is the size of compressed data divides by the size of original data. The following is an example, the left one is an original array, the middle one is a bitmap array and the right one is a compact table. The original array is original data.The bitmap array and the compact table is compressed data. The way of Bitmap Compression is showed in the figure. Each element of a bitmap array occupies one bit. The data type of elements in compact table is the same as in original array.

Assuming the type of elements in original array is char(which occupies 1Byte=8bit each), there are eighteen elements in total. So the size of original array is 1Byte * 18 = 18Byte. After compression the size of bitmap array and compact table are 18 bit and 6Byte. The compression ratio can be calculated as: (18bit + 6Byte) / (18Byte) = 11/24 = 0.458333. Some people may wonder that the Bitmap Compression is not always efficient in some situation. So does fengzlzl. Now given the original array, can you help fengzlzl to calculate compression ratio using Bitmap Compression?



输入:

The first line is an integer T which stands for the number of test cases. For each test case the first line is an integer N which stands for the number of elements in the original array, a blank space, and a string which stands for the data type of elements in original array. The string can only be "char" or "int". The size and range of these two types are the same as signed 8-bit integer or signed 32-bit integer in C/C++, Java. There are N space-seperated integers of the original array in the second line. The i-th integer stands for the element whose subscript is i-1.

Constraints

1 <= N <= 1000



输出:

For each test case output a float number stands for the compression radio, rounded to 6 digits after decimal point.



样例输入:

2
18 char
0 0 0 0 1 1 1 0 0 0 0 2 0 0 0 3 3 3
4 int
-1 -1 -1 -1



样例输出:

0.458333
0.281250




题目大意:

输入整数N,表示原始数组中的元素数,输入原始数组中元素的数据类型的字符串按char(1Byte=8bit each),int(1Byte=32bit each)。按上图图表所示方法计算压缩比。




/*
本题只要理解题意就很好解决 */
#include<stdio.h>
int main()
{
	int t,e,n;
	char str[5];
	int asd[1005];
	int qwe[1005];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %s",&n,str);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&asd[i]);
		}
		e=0;
		for(int i=0;i<n;)						//利用压缩表算数来的位数 
		{
			int k=asd[i];
			qwe[e++]=k;
			int j;
			for(j=i+1;j<n;j++)
			{
				if(k!=asd[j])
				{
					break;
				}
			}
			i=j;
		}
		double p;
		if(str[0]=='c')							//char类型的计算方法 
		p=(n+e*8)*1.0/(n*8);
		else
		p=(n+e*32)*1.0/(n*32);					//int类型的计算方法 
		printf("%.6f\n",p);
	}
	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值