Topic:
Given an array of size m, the number range in the array is 1-n, what if there is a limited bandwidth to transfer an array of that size?
Ideas:
By compressing the array with bitmap bitmap, each number in the array is marked on the bit bit, so that the array size can be compressed many times, each 32-bit int only needs 1bit to represent.
Code:
#include <iostream>#include<string.h>using namespacestd;Char* Compression (int*a,intImaxintN) { intM= (imax-1)/8+1; //int bits[m]={0}; Char*bits=New Char(m); memset (Bits,0, m*sizeof(int)); intByte_offset; intBit_offset; for(intI=0; i<n;i++) {Byte_offset= (a[i]-1)/8; Bit_offset=7-(a[i]-1)%8; Bits[byte_offset]|=1<<Bit_offset; } for(intI=0; i<m;i++) cout<<int(Bits[i]) <<" "; cout<<Endl; returnbits;}void* Depression (Char*b,intm) { //int n=m*8; //int *a=new int (n); for(intI=0; i<m;i++){ for(intj=7; j>=0; j--){ if((B[I]>>J) &1) cout<<i*8+8-j<<" "; }} cout<<Endl;}intMain () {inta[]={1,3,7,9,4,6,8,Ten, -, -}; intn=sizeof(A)/sizeof(a[0]); intimax=0; for(intI=0; i<n;i++){ if(a[i]>IMAX) IMAX=A[i]; } Char*B; B=compression (a,imax,n); intM= (n1)/8+1; Depression (b,m); return 0;}
(algorithm) bitmap bitmap