测试用例说明:这里测试用例的区号为:80、87、88;假设电话号码的范围是[0, 10000), 那么除去区号后其他号的范围就是[0, 100),设计程序如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <stdio.h>
#include <stdlib.h> #include <string.h> /*memset*/ #include <stdint.h> /*int64_t*/ #define MAxNUM 10000 // 总数 #define OTHERNUM 100 // 除去区号数 #define BASENum 100 #define REGIONNUM 3 // 区号个数 #define N 10 typedef struct{ unsigned int bit: 1; } bitType; // 占1位 typedef struct { unsigned int bitsize: 10; // 免费区号(最大999) bitType arrData[OTHERNUM]; // 其他位号码 } numData; int main( void) { numData theData = { 0}; // 初始化bitsize,arrData自动初始化 // memset(theData.arrData, 0, sizeof(theData.arrData)); // 数组清零 int64_t a[N] = { 8780, 8045, 8767, 8880, 8790, 8787, 8845, 8015, 8056, 8813}; int regionNum; // 区号 int64_t otherNum; // 其他号码 for ( int i= 1; i <= REGIONNUM; i++) // 免费区号数目 { for ( int j= 0; j < N; j++) { regionNum = a[j] / BASENum; otherNum = a[j] - regionNum * BASENum; if (regionNum == 80 && i== 1) { theData.bitsize = regionNum; theData.arrData[otherNum].bit = 1; } if (regionNum == 87 && i== 2) { theData.bitsize = regionNum; theData.arrData[otherNum].bit = 1; } if (regionNum == 88 && i== 3) { theData.bitsize = regionNum; theData.arrData[otherNum].bit = 1; } } for ( int k= 0; k < OTHERNUM; k++) { if (theData.arrData[k].bit == 1) printf( "%d%ld\t", theData.bitsize, k); } memset(theData.arrData, 0, sizeof(theData.arrData)); // 数组清零 } return 0; } |
那么,在文本模式下如何在1MB完成所有排序呢? 由于每个区号之后其他数最大情况下有1000w个,相当于1.25MB,3个免费区号就是3*1.25MB,所以,可以采取多趟排序在分别获得了每个免费区号内的所有值之后,在将这些值按照多趟来予以排序。
如何实现快速查找呢?
由于我们已经对所有的数据进行了排序,所以可以使使用二分查找的方法以实现元素的准确定位。