Bitmap

来源:互联网 发布:水仙花数的算法流程图 编辑:程序博客网 时间:2024/05/20 01:10

描述:

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);elsep=(n+e*32)*1.0/(n*32);//int类型的计算方法 printf("%.6f\n",p);}return 0;} 



原创粉丝点击