C,259字节
接受无注释的PPM文件。
double*f,r,g,b;x,y,z,i;char*s="%d %d %d";main(a,_){(a-2)?(feof(f)?0:(fscanf(f,s,&x,&y,&z),r+=(x-r)/i,g+=(y-g)/i,b+=(z-b)/i++,main(0,0))):(f=fopen(((char**)_)[1],"r"),fscanf(f,"%*s%*d%*d%*d"),r=g=b=0.,i=1,main(0,0),printf(s,(int)r,(int)g,(int)b),fclose(f));}
处理
初始代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *f = fopen(argv[1],"r");
int w,h,d,x,y,z,i;
double r,g,b;
fscanf(f,"%*s %d %d %d",&w,&h,&d);//get width, height, depth, ignore P6
r = g = b = 0.0; //zero r, g, and b totals
for (i=1; i<=w*h; ++i) {
fscanf(f,"%d %d %d",&x,&y,&z);//get next pixel
r+=(x-r)/i;//update averages
g+=(y-g)/i;
b+=(z-b)/i;
}
printf("%d %d %d",(int)r,(int)g,(int)b);//print result
fclose(f);
return 0;
}
修剪变量并删除循环:
double r,g,b;
FILE *f;
int i;
int main(int argc, char *argv[])
{
if (argc==2) { // {./me} {file.ppm}
f = fopen(argv[1],"r");
fscanf(f,"%*s%*d%*d%*d");//drop info
r = g = b = 0.0;
i = 1;
main(0,0);//begin load loop
printf("%d %d %d",(int)r,(int)g,(int)b);
fclose(f)
} else {
if (feof(f)) return 0;
fscanf(f,"%d%d%d",&x,&y,&z);
r+=(x-r)/i;
g+=(y-g)/i;
b+=(z-b)/i;
i++;
main(0,0);
}
return 0;
}
从那里,我将各种语句组合成单个return语句。删除了它和任何其他无关的类型信息,重命名了变量,并剪切了空白。