// Question: find non-repeated integers among the 0.25 billion integers. The memory is insufficient to accommodate these 0.25 billion integers. # Include <stdio. h> # include <memory. h>/* 2 bits are allocated for each number. 00 indicates that the number does not exist. 01 indicates that the number appears once. 10 indicates multiple times. 11 indicates that the number of BITs does not exist. 2-bitmap is stored in the char array, the ing relationship between the memory size and the memory size is not considered as follows: | 00 00 00 00 | // ing | 3 2 1 0 | 00 00 00 00 | // ing | 7 6 5 4 | ...... | 00 00 00 00 | */unsigned char flags [1000]; // custom array size, the number of times each integer appears 1000*4 = 4000 integer unsigned get_val (INT idx) {int I = idx/4; // determine the array position Int J = idx % 4; // determine the 8-bit position unsigned ret = (flags [I] & (0x3 <(2 * j)> (2 * j ); // 0x3 hexadecimal notation: 00 00 00 11 an integer can be controlled to obtain the number of occurrences of an integer return ret ;} /* bit operation test */unsigned set_val (INT idx, unsigned int Val) {int I = idx/4; Int J = idx % 4; unsigned TMP = (flags [I] & ~ (0x3 <(2 * j) & 0xff) | (Val % 4) <(2 * j) & 0xff ); // or operation, the other BITs remain unchanged, and the flags [I] = TMP; return 0;} unsigned add_one (INT idx) to be modified are reset) {If (get_val (idx)> = 2) {return 1 ;}else {set_val (idx, get_val (idx) + 1); Return 0 ;}} // only test non-negative values. // if negative values are considered, a 2-bitmap array is required. int A [] = {1, 3, 5, 7, 9, 1, 3, 5, 7, 1, 3, 5, 3, 0}; int main () {int I; memset (flags, 0, sizeof (flags); printf ("original array:"); for (I = 0; I <sizeof (a)/sizeof (INT); ++ I) {printf ("% d", a [I]); add_one (A [I]);} printf ("\ r \ n"); printf ("number of occurrences only once:"); for (I = 0; I <100; ++ I) {If (get_val (I) = 1) printf ("% d", I);} printf ("\ r \ n"); Return 0 ;} in addition to using 2-bitmap to count tags, you can also use two 1-Bitmap (if you consider positive and negative numbers, four 1-Bitmap)
Transferred from: Internet