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;
} 



發佈了31 篇原創文章 · 獲贊 27 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章