hdu - 1225:Football Score

   这是个简单的字符串处理题目。看题目,数据应该不是很大,直接暴力处理可以过。如果为了加快搜索速度,在中间输入过程中排序,
再二分很麻烦,速度也快不了多少,因为只是输入的过程中需要查找。但是,这个题其实很好用map做,代码量可以少很多,也很简洁。
   写这篇blog的目的是为了提醒自己,容易题再这样错下去,真的很伤人心,学什么都没必要了,当时打算继续搞ACM的目的之一就是
为了提高代码正确率。这个题,不仅细节部分没看清楚,而且写代码时候把比较函数里面的one.nLost写成了one.nGet,查错了1个多
小时,还让队友帮忙查错了好久,真的很无语。写程序确实可以debug,但是这也让我养成了很严重的依赖debug的习惯。
   人生不可以debug,人生不可以重来。记得以前很多次很多事情就是开始无所谓,后面悲催到底,无限后悔。

   代码如下:
   1 #include <stdio.h>
  2 #include <string.h>
  3 #include <string>
  4 #include <map>
  5 #include <vector>
  6 #include <algorithm>
  7 #define MAX (100)
  8 using std::map;
  9 using std::string;
 10 using std::vector;
 11 using std::sort;
 12 
 13 struct INFO
 14 {
 15     INFO()
 16     {
 17         nScore = nGet = nLost = 0;
 18     }
 19 
 20     string strName;
 21     int nScore;
 22     int nGet;
 23     int nLost;
 24     bool operator < (const INFO& one) const
 25     {
 26         if (nScore != one.nScore)
 27         {
 28             return nScore > one.nScore;
 29         }
 30         else if (nGet - nLost != one.nGet - one.nLost)//这里把one.nLost写成了one.nGet
 31         {
 32             return nGet - nLost > one.nGet - one.nLost;
 33         }
 34         else if (nGet != one.nGet)
 35         {
 36             return nGet > one.nGet;
 37         }
 38         else
 39         {
 40             return strName < one.strName;
 41         }
 42     }
 43 };
 44 
 45 int main()
 46 {
 47     int nN;
 48 
 49     //freopen("in.txt", "r", stdin);
 50     //freopen("out.txt", "w", stdout);
 51     while (scanf("%d", &nN) == 1)
 52     {
 53         int nLast = nN * (nN - 1);
 54         char szOne[MAX];
 55         char szTwo[MAX];
 56         int nOne, nTwo;
 57 
 58         map<string, INFO> myMap;
 59         for (int i = 0; i < nLast; ++i)
 60         {
 61             scanf("%s %*s %s %d:%d", szOne, szTwo, &nOne, &nTwo);
 62             //printf("%s %s %d %d\n", szOne, szTwo, nOne, nTwo);
 63             
 64             string strOne = szOne;
 65             myMap[strOne].strName = strOne;
 66             myMap[strOne].nGet += nOne;
 67             myMap[strOne].nLost += nTwo;
 68             
 69             string strTwo = szTwo;
 70             myMap[strTwo].strName = strTwo;
 71             myMap[strTwo].nGet += nTwo;
 72             myMap[strTwo].nLost += nOne;
 73 
 74             if (nOne > nTwo)
 75             {
 76                 myMap[strOne].nScore += 3;
 77             }
 78             else if (nOne == nTwo)
 79             {
 80                 myMap[strOne].nScore += 1;
 81                 myMap[strTwo].nScore += 1;
 82             }
 83             else
 84             {
 85                 myMap[strTwo].nScore += 3;
 86             }
 87         }
 88         
 89         map<string, INFO>::iterator it;
 90         vector<INFO> myVt;
 91         for (it = myMap.begin(); it != myMap.end(); it++)
 92         {
 93             myVt.push_back(it->second);
 94         }
 95         
 96         sort(myVt.begin(), myVt.end());
 97         for (int i = 0; i < myVt.size(); ++i)
 98         {
 99             printf("%s %d\n", myVt[i].strName.c_str(), myVt[i].nScore);
100         }
101         printf("\n");
102     }
103     
104     return 0;
105 }

posted on 2012-03-14 21:23 yx 阅读(1322) 评论(2)  编辑 收藏 引用 所属分类: 字符串

评论

# re: hdu - 1225:Football Score 2012-03-17 10:44 bigrabbit

人生不可以debug,人生不可以重来。说得好~  回复  更多评论   

# re: hdu - 1225:Football Score 2012-03-17 18:04 远行

哈哈,有感而发@bigrabbit
  回复  更多评论   


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


<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

常用链接

留言簿(3)

随笔分类

随笔档案

me

好友

同学

网友

搜索

最新评论

阅读排行榜

评论排行榜