我有一个代码,它从 stdin 读取大约 (10^5) int(s) ,然后在执行 ## 后将它们输出到 stdout 上。我通过使用“setvbuf”和使用“fgets_unlocked()”读取行来处理输入部分,然后解析它们以获得所需的 int(s)。我有两个无法解决的问题:
1.)当我在标准输出上打印 int(s) 500 万时,它花费了大量时间:有没有办法减少这个(我尝试使用 fwrite() 但由于使用 fread 的原因,o/p 打印了不可打印的字符读入 int 缓冲区)
2.)在解析 int(s) 的输入后,说“x”,我实际上通过在循环中对 no 执行 %(mod) 来找到除数的 no。(参见下面的代码):也许这也是一个我的代码超时的原因:对此的任何建议都需要改进。非常感谢这实际上是来自http://www.codechef.com/problems/PD13的问题
# include <stdio.h>
# define SIZE 32*1024
char buf[SIZE];
main(void)
{
int i=0,chk =0;
unsigned int j =0 ,div =0;
int a =0,num =0;
char ch;
setvbuf(stdin,(char*)NULL,_IOFBF,0);
scanf("%d",&chk);
while(getchar_unlocked() != '\n');
while((a = fread_unlocked(buf,1,SIZE,stdin)) >0)
{
for(i=0;i<a;i++)
{
if(buf[i] != '\n')
{
num = (buf[i] - …Run Code Online (Sandbox Code Playgroud)