å²ä¸æ大ã®ã³ã¼ãã£ã³ã°ã¹ãã«å¤å®
あなたのスキルで飯は食えるか? 史上最大のコーディングスキル判定 (1/2) - ITmedia エンタープライズ
æ®éã«æ¸ãã¦ãé¢ç½ããªãã®ã§ C ã® Short Coding ã§ï¼749Bâ685Bâ637Bï¼æè¦æéã¯1æéãããã§ãï¼ã¢ã«ã´ãªãºã ã縮ãæ¹ãããã¾ãåã£ã¦ãªãã§ãï¼
æ¡ä»¶
ä»æ§ãã¯ã£ããæ¸ãã¦ããªãã®ã§ä¾é¡ããæ¨æ¸¬ãã¦é©å½ã«æ±ºãã¾ããï¼
- å ¥åã¯1~9ãæé ã«13å並ãã æåå
- å ¥åãããæååã«ããã¦åã ã®æ°åã¯0ã3å
- å ¥åã¯æ¨æºå ¥åããè¡ããã
- ä¸å¯¾åã¯èããªã
ã«ã³ãã£ã³å¾ ã¡ã¯èããªã- 32-bit int, LE
ã³ã¼ã
k,l,r;u[99999];*p=u+49;char v[99999],*s=v,*z="%s(%d%d)[%d%d]\n";e(n,o){memcpy(p+=9,n,36);memcpy(s+=24,o,24);}f(a,i,j){int d,*n=p,*o=s,b=1,c=i+49;for(r=k=0;k<9;k++)for(l=0;l++<n[k];d=u[1]-u[0])u[r++]=k+1;r<2&&printf("%s[%d]\n",s,u[0]);r==2&&(!d&&printf(z,s,u[0],u[1],a,a),d<3&&printf(z,s,a,a,u[0],u[1]));r>2&&(!a&&n[i]>1&&(e(n,o),p[i]-=2,f(i+1,i+1,j),b=0),i<7&&n[i]*n[i+1]*n[i+2]>0&&(e(n,o),p[i]--,p[i+1]--,p[i+2]--,s[j]=40,s[j+1]=c,s[j+2]=c+1,s[j+3]=c+2,s[j+4]=41,f(a,i,j+5)),n[i]>2&&(e(n,o),p[i]-=3,s[j]=40,s[j+1]=s[j+2]=s[j+3]=c,s[j+4]=41,f(a,i+1,j+5),b=0),b&&i<8&&(e(n,o),f(a,i+1,j)));}main(){for(gets(v);k<13;u[v[k++]]++);f(0,0,0);}
çµæ
/Users/yuyarin/Program/temp% gcc -m32 majiang.c majiang.c:1: warning: data definition has no type or storage class majiang.c:1: warning: data definition has no type or storage class majiang.c:1: warning: data definition has no type or storage class majiang.c: In function âeâ: majiang.c:1: warning: incompatible implicit declaration of built-in function âmemcpyâ majiang.c:1: warning: passing argument 2 of âmemcpyâ makes pointer from integer without a cast majiang.c:1: warning: passing argument 2 of âmemcpyâ makes pointer from integer without a cast majiang.c: In function âfâ: majiang.c:1: warning: initialization from incompatible pointer type majiang.c:1: warning: incompatible implicit declaration of built-in function âprintfâ /Users/yuyarin/Program/temp% ./a.out < case1 warning: this program uses gets(), which is unsafe. (111)(222)(888)(99)[45] /Users/yuyarin/Program/temp% ./a.out < case2 warning: this program uses gets(), which is unsafe. (123)(123)(567)(99)[55] (123)(123)(567)(55)[99] (123)(123)(555)(99)[67] /Users/yuyarin/Program/temp% ./a.out < case3 warning: this program uses gets(), which is unsafe. (123)(123)(123)(555)[9] (111)(222)(333)(555)[9] /Users/yuyarin/Program/temp% ./a.out < case4 warning: this program uses gets(), which is unsafe. (123)(234)(888)(999)[4] (123)(888)(999)(44)[23] (234)(234)(888)(999)[1] /Users/yuyarin/Program/temp% ./a.out < case5 warning: this program uses gets(), which is unsafe. (345)(678)(999)(11)[12] (123)(456)(789)(99)[11] (123)(456)(789)(11)[99] (123)(456)(999)(11)[78] (123)(678)(999)(11)[45] (111)(234)(567)(99)[89] (111)(234)(567)(999)[8] (111)(234)(678)(999)[5] (111)(234)(789)(99)[56] (111)(345)(678)(999)[2] (111)(456)(789)(99)[23] /Users/yuyarin/Program/temp% ./a.out < case6 warning: this program uses gets(), which is unsafe. (123)(234)(234)(11)[79] (123)(123)(123)(44)[79] (111)(222)(333)(44)[79]
解説
æçã®ããããã®çã®æ°åã®æ°ãæ°ãã¦ï¼å¯¾åï¼é åï¼å»åãä½ãããã©ãããå帰çã«èª¿ã¹ã¦ããï¼
æçã«æ®ã£ãçã®æ°åã®æ°ã¯ u ã« 9 ã¤ãã¤è¨é²ï¼p, n ããã¤ã³ã¿ï¼åºåããæåå㯠'\0' å«ã㦠24 æåãªã®ã§ v ã« 24 åãã¤è¨é²ï¼s, o ããã¤ã³ã¿ï¼å¯¾åï¼é åï¼å»åãä½ããå ´åã¯å 容ã e() ã§ã³ãã¼ãã¦ãã¤ã³ã¿ãé²ãï¼å 容ãã¢ãããã¼ããã¦ããï¼
// ã«ã¼ãæå k,l,r; // æ°å x ã®çãããã¤æ®ã£ã¦ãããï¼u[49] ãã 9 ã¤ãã¤ä½¿ç¨ // u[0]~u[12] ã¯å帰é¢æ°å ã§æ®ã£ãçã調ã¹ãã®ã«ä½¿ç¨ u[99999]; // u[] ã«ãããç¾å¨ä½¿ç¨ãã¦ããé åé¨åã¸ã®ãã¤ã³ã¿ *p=u+49; // åºåããæåå 24 åãã¤ä½¿ç¨ char v[99999], // v[] ã«ãããç¾å¨ä½¿ç¨ãã¦ããé åé¨åã¸ã®ãã¤ã³ã¿ *s=v, *z="%s(%d%d)[%d%d]\n"; // ã¡ã¢ãªã³ãã¼å¦ç e(n,o){ // u[] ä¸ã§ç¾å¨æ³¨ç®ãã¦ããä½ç½®ã®ãã¤ã³ã¿ n ããï¼ // 次ã«èª¿ã¹ãããã« p+9 ã¸å 容ãã³ãã¼ãã¦ãã¤ã³ã¿ãé²ãã memcpy(p+=9,n,36); // v[] ä¸ã§åæ§ memcpy(s+=24,o,24); } // å帰å¦ç // a: é ã«ãªã£ã¦ããçã®æ°åï¼ç¡ããã° 0 // i: 注ç®ãã¦ããçã®æ°åï¼ u[] ã®ã¤ã³ããã¯ã¹ã«ä½¿ç¨ // j: åºåããæååã®æå¾ã®ä½ç½®ï¼v[] ã®ã¤ã³ããã¯ã¹ã«ä½¿ç¨ f(a,i,j) { int d, // æ®ãï¼ã¤ã®çã®å·® *n=p,*o=s, // ãã¤ã³ã¿ä¿å b=1, // 次ã®çã調ã¹ãããã©ã° c=i+49; // c = i+1+'0' // æªå¦çã®çã®æ°åãå°ããé ã«é å u[0]~u[8] ã¸è¨é² // ã«ã¼ãçµäºå¾ r ã¯æªå¦çã®æçã®æ°ã«ãªã£ã¦ãã for(r=k=0;k<9;k++) for(l=0;l++<n[k];d=u[1]-u[0]) u[r++]=k+1; // åé¨å¾ ã¡ // if(r==1) r<2 && printf("%s[%d]\n",s,u[0]); // 2ã¤å¾ ã¡ r==2 && ( // if(d==0) ã¤ã¾ã if(u[0]==u[1]) // ãªã£ã³ã¡ã³å¾ ã¡ã¯é ã¨æ®ãã®ï¼éããã !d && printf(z,s,u[0],u[1],a,a), // if(d<=2) // æ®ãï¼ã¤ã®çã®å·®ã // 0: ã·ã£ãå¾ ã¡ // 1: ãã³ãã£ã³å¾ ã¡, ãªã£ã³ã¡ã³å¾ ã¡ // 2: ã«ã³ãã£ã³å¾ ã¡ d<3 && printf(z,s,a,a,u[0],u[1]) ); // çã3ã¤ä»¥ä¸æ®ã£ã¦ããå ´å r>2 && ( // ç i+1 ãé // n[i] 㯠ç i+1 ã®æ®ãæ° !a&&n[i]>1 && ( e(n,o), // ãã¼ã¿ã®ã³ãã¼ã¨ãã¤ã³ã¿p,sé²ã p[i]-=2, // çã®æ°ãæ¸ãã f(i+1,i+1,j), // ç i+1 ãé ã«ãã¦å帰ï¼ç i+2 ã«æ³¨ç® b=0 // ãã©ã° ), // ç i+1 ããé å i<7&&n[i]*n[i+1]*n[i+2]>0 && ( e(n,o), p[i]--,p[i+1]--,p[i+2]--, // çã®æ°ãæ¸ãã s[j]=40, // '(' s[j+1]=c, // i+1+'0' s[j+2]=c+1, // i+2+'0' s[j+3]=c+2, // i+3+'0' s[j+4]=41, // ')' f(a,i,j+5) // å帰ãããï¼ç i+1 ã«æ³¨ç®ããã¾ã¾ ), // ç i+1 ãå»å n[i]>2 && ( e(n,o), p[i]-=3, s[j]=40, s[j+1]=s[j+2]=s[j+3]=c, s[j+4]=41, f(a,i+1,j+5), // ç i+2 ã«æ³¨ç®ãé²ãã¦å帰 b=0 ), // é ã¨å»åããªãã£ãã次ã®ç㸠b&&i<8 && ( e(n,o), f(a,i+1,j) ) ); } main(){ // æ¨æºå ¥åããä¸è¡èªã¿è¾¼ã¿ // u[49]~u[57] ã¸çã®åæ°ãè¨é² for(gets(v);l<13;u[v[l++]]++); // åæç¶æ ã§å帰é¢æ°ã®å®è¡ f(13,0,0,0); }
ææ³
å¾ããä»ã®äººã®ã³ã¼ããèªãã§ããã¨ã«ã³ãã£ã³å¾ ã¡ã¨ãããã®ãããããããï¼åé¡ä¾ã«ããªãã£ãã®ã§ããã¯èãã¦ããªãã£ãï¼ãã¨ã§ä»ãå ãããï¼
âä»ãå ãã
å²ä¸æ大ã®ã³ã¼ãã£ã³ã°ã¹ãã«å¤å®ã¨ããããã«ã¯ããå°ãåé¡å®ç¾©ãæ確ã«ãã¦æ¬²ããã£ãï¼éº»éã¨ããåèªããããªãåºãã¦å°éç¨èªã§åé¡è§£èª¬ãããã¨ã§ï¼éº»éãç¥ããªã人ãã¨ã£ã¤ãã«ããå°è±¡ãåãã¦ããã®ãç®æãã¦ããã®ã§æ®å¿µï¼
è¨èªã«å¯¾ããã³ã¡ã³ã
ãåçããã ããæ¹ã¯ããã®åé¡ã«ã¤ãã¦ãèªåãç¨ããè¨èªã使ã£ã¦ããã£ããªã¨ãã¿ãã¿æãé¨åãªã©ãã³ã¡ã³ãããã ããã°å¹¸ãã§ãã #makeplex
æé»ã®äºè§£ã§åã int ã«ãªãã®ã¯çã¹ãã¼ã¹ã§ã¨ã³ã§è¯ãã§ããï¼ãããã #include ããªãã¦ãçµã¿è¾¼ã¿é¢æ°ã使ããã®ãè¯ãã§ãï¼æ¹è¡ãæèã«å½±é¿ãä¸ããªãã®ãç´ æµã ã¨æãã¾ãï¼ã§ããããæ¼ç®åãæ¯è¼æ¼ç®åãããåªå é ä½ãä½ãã®ã¯å«ã§ãï¼
ä»ã® Short Coder
300Bï¼ï¼ï¼ãããæ¬å½ã® Golf ã§ããï¼åã®ã¯æ®éã«è¨è¿°ããã¢ã«ã´ãªãºã ãç°¡åã«çãããã ããªã®ã§ï¼ï¼ï¼