May the force be with you!
posts - 52,  comments - 33,  trackbacks - 0
好久没做题了,今天为了周日的地大比赛,热热身,把pku的1002这个简单题切了,虽然先tle了一次。。。
教训:
     对于这种模拟性质的题,应该尽可能的让思路简单一点,不要想的太复杂;
     另外,最好一个模块一个模块的写,编写边注释,这样思路比较不容易乱。
                                                                  Simbaforrest
                                                                      2007.12.6

代码比较简单,我都加了注释:
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 const int maxn=100010;
  5 typedef struct tagNum
  6 {
  7   int num;
  8   char in[50];
  9 }Num;
 10 typedef struct tagTel
 11 {
 12   Num tel[maxn];
 13   int t;
 14 }Tel;
 15 Tel T;
 16 
 17 int cmp(const void *a, const void *b)
 18 {
 19   Num *= (Num *)a;
 20   Num *= (Num *)b;
 21   return c->num - d->num;
 22 }
 23 
 24 void Format(Num &N)
 25 {
 26   char *pMove,*pSet;
 27   pMove = pSet = N.in;
 28   while(pMove != NULL )
 29   {
 30     if(pSet - N.in == 7)
 31       break;
 32     //change pMove to number
 33     switch(*pMove)
 34     {
 35       case 'A':case 'B':case 'C':
 36         *pMove = '2';break;
 37       case 'D':case 'E':case 'F':
 38         *pMove = '3';break;
 39       case 'G':case 'H':case 'I':
 40         *pMove = '4';break;
 41       case 'J':case 'K':case 'L':
 42         *pMove = '5';break;
 43       case 'M':case 'N':case 'O':
 44         *pMove = '6';break;
 45       case 'P':case 'R':case 'S':
 46         *pMove = '7';break;
 47       case 'T':case 'U':case 'V':
 48         *pMove = '8';break;
 49       case 'W':case 'X':case 'Y':
 50         *pMove = '9';break;
 51     }
 52     
 53     //set the pSet
 54     if(*pMove<='9' && *pMove>='0')
 55     {
 56       *pSet = *pMove;
 57       pSet++;
 58       //lbl_3++;
 59     }
 60     
 61     //move the pMove
 62     pMove++;
 63   }
 64   //set the end of char
 65   N.in[7]='\0';
 66   
 67   //set value of tel
 68   int sum = 0;
 69   int w = 1;
 70   for(int i=6; i>=0; i--)
 71   {
 72     //if(i!=3)
 73     {
 74       sum+=(N.in[i]-'0')*w;
 75       w*=10;
 76     }
 77   }
 78   N.num = sum;
 79   //while(1);
 80 }
 81 
 82 bool out()
 83 {
 84   bool has = 0;
 85   int time = 1;
 86   for(int i=1; i<=T.t; i++)
 87   {
 88     if(i!=T.t)
 89     {
 90       if(T.tel[i].num == T.tel[i-1].num)
 91       {
 92         time++;
 93       }
 94       else
 95       {
 96         if(time>1)
 97         {
 98           has = 1;
 99           for(int j=0;j<7;j++)
100           {
101             if(j==3)
102               printf("-");
103             printf("%c",T.tel[i-1].in[j]);
104           }
105           printf(" %d\n",time);
106           time = 1;
107         }
108       }
109     }
110     else
111     {
112       if(time>1)
113         {
114           has = 1;
115           for(int j=0;j<7;j++)
116           {
117             if(j==3)
118               printf("-");
119             printf("%c",T.tel[i-1].in[j]);
120           }
121           printf(" %d\n",time);
122           time = 1;
123         }
124     }
125   }
126   return has;
127 }
128 
129 int main()
130 {
131   int t;
132   while(scanf("%d",&t)!=EOF)
133   {
134     T.t = t;
135     int i=0;
136     while(t--)
137     {
138       scanf("%s",T.tel[i].in);
139       Format(T.tel[i]);
140       i++;
141     }
142     qsort(T.tel,T.t,sizeof(Num),cmp);
143     if(!out())
144       printf("No duplicates.\n");
145   }
146   return 0;
147 }
148 



下面是littlekid的:

  1 /*******************************************
  2 Source Code
  3 Problem: 1002        User: LittleKid
  4 Memory: 1248K        Time: 1357MS
  5 Language: G++        Result: Accepted
  6 ************************************************/
  7 # include <stdio.h>
  8 # include <stdlib.h>
  9 # include <string.h>
 10 
 11 const int tt[]={0,1,10,100,1000,10000,100000,1000000,10000000};
 12 
 13 int n;
 14 char a[n][7];
 15 int num[n];
 16 
 17 void init()
 18 {
 19     scanf("%d",&n);
 20     for (int i=0;i<n;i++)
 21     {
 22         int k=0;
 23         while (k<7)
 24         {
 25               scanf("%c",&a[i][k]);
 26               if (a[i][k]!='-' && a[i][k]!='\n') k++;
 27         }
 28     }
 29 }
 30 
 31 int cmp(const void *a,const void *b)
 32 {
 33      return (*(int *)a-*(int *)b);
 34 }
 35 
 36 void trans()
 37 {
 38      //Turn upcase chars into numbers in a according to the rules.
 39     for (int i=0;i<n;i++)
 40     {
 41         num[i]=0;
 42         for (int j=0;j<7;j++)
 43         {
 44             switch (a[i][j])
 45             {
 46                    case 'A':case 'B':case 'C':a[i][j]='2';
 47                         break;
 48                    case 'D':case 'E':case 'F':a[i][j]='3';
 49                         break;
 50                    case 'G':case 'H':case 'I':a[i][j]='4';
 51                         break;
 52                    case 'J':case 'K':case 'L':a[i][j]='5';
 53                         break;
 54                    case 'M':case 'N':case 'O':a[i][j]='6';
 55                         break;
 56                    case 'P':case 'R':case 'S':a[i][j]='7';
 57                         break;
 58                    case 'T':case 'U':case 'V':a[i][j]='8';
 59                         break;
 60                    case 'W':case 'X':case 'Y':a[i][j]='9';
 61                         break;
 62             }
 63             num[i]+=(a[i][j]-'0')*tt[7-j];
 64         }
 65     } 
 66 }
 67 
 68 void output()
 69 {
 70     int t;
 71     bool flag = true;
 72     t=1;   
 73     for (int i=1;i<n;i++)
 74     {
 75         if (num[i]!=num[i-1])
 76         {
 77            if (t>1)
 78            {
 79                printf("%03d-%04d %d\n",num[i-1]/10000,num[i-1]%10000,t);
 80                flag=false;
 81            } 
 82            t=0;
 83         }
 84         t++;
 85     }
 86     if (t>1)
 87     {
 88         printf("%03d-%04d %d\n",num[n-1]/10000,num[n-1]%10000,t);
 89         flag=false;
 90     } 
 91     if (flag) printf("No duplicates.\n");
 92 }
 93 
 94 int main()
 95 {
 96     init();
 97     trans();
 98     qsort(num,n,sizeof(num[0]),cmp);
 99     output();
100     return 0;
101 }
102 


posted on 2007-12-06 19:27 R2 阅读(272) 评论(0)  编辑 收藏 引用 所属分类: Problem Solving

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


你是第 free hit counter 位访客




<2007年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(4)

随笔分类(54)

随笔档案(52)

文章档案(1)

ACM/ICPC

技术综合

最新随笔

搜索

  •  

积分与排名

  • 积分 - 62043
  • 排名 - 356

最新评论

阅读排行榜

评论排行榜