2012/4/17

Google Code Jam 2012 資格賽 第二題 Problem B. Dancing With the Googlers 重構跟短碼之後,可讀性?

重構跟短碼之後,可讀性?

#include "stdafx.h"

 

void main()

{

        int table[2][3] = {    {1, 2, 2}, {0, 0, 1} };

        int line,n,s,p,i,j,t,x; 

        scanf("%d", &line);

        for(i=0;i<line;i++)

        {

                int count=0;

                scanf("%d %d %d", &n, &s, &p);

                for(j=0;j<n;j++)

                {

                        scanf("%d", &t);

                        if(!(t || p)) count++;

                        if(!t) continue;

                        if( (x = p-t/3) > 2 ) continue;                  

                        if( x <= 0 ) { count++; continue; }                   

                        if( table[x-1][t-t/3*3] ==1 ) { count += s-->0;  continue; }

                        if( table[x-1][t-t/3*3] ==2 ) count ++;                    

                }

                printf("Case #%d: %d\n", i+1,count);

        }      

}

 

From:

 

Smail Set 一次 10分。

Large Set 一次 15分。

花費時間: 1hr 15min

 

http://code.google.com/codejam/contest/1460488/dashboard#s=p1&a=1

 

 

題目分析:

這一題在考題目理解能力,有人說數學的符號很重要,沒萊布尼茲發明微積分符號,

就沒有後來蓬勃的發展,那解程式符號與圖型也很重要,不然題目落落長,看了三遍也不懂。

 

看似很複雜,其實整體只有六種一般狀況加上一種例外。

六種一般狀況用表格表式,因為狀況單純所以用查表的,

就是table的第一與第二個column

 

 

 

From:

 

// DancingWithGooglers.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

 

void main()

{

        // 0=>x, 1=>sprz, 2=>count++

        int table[3][4] =

        {

                {0, 1, 0, 0},

                {0, 2, 0, 0},

                {0, 2, 1, 0}

        };

 

        int line,n,s,p,i,j,t,m,q,r,x;

        int count;

        scanf("%d", &line);

        for(i=0;i<line;i++)

        {

                count=0;

                scanf("%d %d %d", &n, &s, &p);

                for(j=0;j<n;j++)

                {

                        scanf("%d", &t);

                        if(t==0) //例外

                        {

                                if(p==0) count++;

                                continue;

                        }

                        m=t/3;

                        x = p-m;

                        if( x > 2 ) continue;

                        q = t-m*3;

 

                        //printf("n,s,p,t,m=t/3,x=p-m,q,table[q][x]=%d,%d,%d,%d,%d,%d,%d,%d\n",n,s,p,t,m,x,q,table[q][x]);

                        if(x>0)

                        {

                                r = table[q][x];

                                if(r==1) { //sprz

                                        if(s>0) {

                                                count ++;

                                                s--;

                                        }

                                }

                                if(r==2)

                                {

                                        count ++;

                                        //count += !(table[q][x]==0);

                                }

                        }else{

                                count++;                 

                        }

                }

                printf("Case #%d: %d\n", i+1,count);

        }      

}

沒有留言:

張貼留言