是否有可能编写一个包含数千个或更少字符的程序来生成所有可能的 128 KB 文件

问题描述 投票:0回答:1

我在德国数学教科书[Konkrete Mathematik (nicht nur) für Informatiker].

我知道这是不可能的,但我无法提出令人信服的论据。我认为有些字节序列无法计算,因此您至少需要与要写入文件的字节数一样多。

希望有人能提供更准确更有说服力的论据。谢谢。

我试图在互联网上找到答案,但没有成功。我希望得到一个计算理论类的答案。

computation-theory
1个回答
1
投票

这是可能的——作为演示,下面是一个 615 字节的 C 程序,它将生成每个可能的

numBytes
字节长的文件(其中
numBytes
是您可以在第 7 行设置的常量)。它将每个可能文件的内容作为一行十六进制字节值打印到标准输出。

请注意,我将

numBytes
设置为 3,这样当您运行该程序时,您可以看到它在合理的时间内运行完成。如果需要,您可以将
numBytes
更改为128KB (128*1024),但是程序完成需要很长时间:)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
   const int numBytes = 3; //128*1024;

   unsigned char * buf = (unsigned char *) malloc(numBytes);
   memset(buf, 0, numBytes);

   int keepGoing = 1;
   while(keepGoing)
   {  
      for (int i=0; i<numBytes; i++) printf("%02x ", buf[i]);
      printf("\n");
      
      unsigned char * op = buf+(numBytes-1);
      unsigned char * p  = op;
      while(*p == 0xFF)
      {  
         (*(--p))++;
         if ((p == buf)&&(*p == 0x00)) {keepGoing = 0; break;}
      }
      (*op)++;
   }
   free(buf);

   printf("done!\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.