#include <stdio.h> #include <stdlib.h> #include <time.h>
#define BEGIN 0 #define END 10000000 #define NUM (END - BEGIN) #define BIT 32 #define SHIFT 5 #define MASK 0x1f
int a[1 + NUM/BIT];
void set(int i) { a[i>>SHIFT] |= (1 << (i & MASK)); }
void init(int i) { a[i>>SHIFT] &= ~(1 << (i & MASK)); }
int test(int i) { return (a[i>>SHIFT] & (1 << (i & MASK))); }
void main() { FILE *fpIn, *fpOut = NULL; char cBuffer[8] = {0}; char cEnter = '\n'; char *pcTemp = NULL; int iStrto,i = 0; clock_t start,finish = 0; double duration;
start = clock();
fpIn = fopen("in.txt", "r"); fpOut = fopen("out.txt", "w+");
while(!feof(fpIn)) { *cBuffer = '\0'; pcTemp = cBuffer;
fread(pcTemp, sizeof(unsigned char), 1, fpIn);
if(*pcTemp == '\0') { break; }
while(*pcTemp != cEnter) { pcTemp++; fread(pcTemp, sizeof(unsigned char), 1, fpIn); }
*pcTemp = '\0';
iStrto = atoi(cBuffer); set(iStrto); } for(i=BEGIN; i<END; i++) { if(test(i)) { itoa(i, cBuffer, 10); fwrite(cBuffer, sizeof(unsigned char), strlen(cBuffer), fpOut); fwrite(&cEnter, sizeof(unsigned char), 1, fpOut); } }
fclose(fpIn); fclose(fpOut);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "%f seconds\n", duration ); }
void init_in() { FILE *fpIn = NULL; int i = 0; char cBuffer[8] = {0}; char cEnter = '\n'; fpIn = fopen("in.txt", "w+"); for(i=BEGIN; i<END; i++) { itoa(i, cBuffer, 10); fwrite(cBuffer, sizeof(unsigned char), strlen(cBuffer), fpIn); fwrite(&cEnter, sizeof(unsigned char), 1, fpIn); } fclose(fpIn); }
|