CodeForce 19A World Football Cup (string类应用)

客官°小女子只卖身不卖艺 2022-09-17 11:19 134阅读 0赞

十足水题,拿来练手速的,做完之后从别人那里学了点东西:

STL的string类中有如下用于查找的函数

  1. find_first_of (char *s,int pos)
  2. //从pos开始查找第一个s字符集的某字符出现在串中的位置,成功时返回所在位置
  3. find_last_of
  4. find_first_not_of
  5. find_last_not_of

题意:足球比赛,给出n(保证是偶数)支队伍的队名和的对阵结果,有一半的队伍出线,按照字典序输出出线的队伍。排序规则:得分胜3平1负0,分数相等比较净胜球,净胜球相等比较进球数。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. #include <cstring>
  5. #include <string>
  6. #include <algorithm>
  7. using namespace std;
  8. struct Data
  9. {
  10. int num;
  11. string name;
  12. int dif,score;
  13. Data () {num=0,dif=0,score=0;}
  14. ~Data () {}
  15. bool operator < (const Data& b) const
  16. {
  17. if (num!=b.num) return num>b.num;
  18. else if (dif!=b.dif) return dif>b.dif;
  19. else if (score!=b.score) return score>b.score;
  20. return false;
  21. }
  22. }data[105];
  23. bool cmp (const Data &a,const Data &b)
  24. {
  25. if (a.name<b.name) return true;
  26. return false;
  27. }
  28. char str[105],str2[105];
  29. int n;
  30. map<string,int> mp;
  31. void In ()
  32. {
  33. int i;
  34. scanf("%s",str);
  35. for (i=0;true;i++)
  36. if (str[i]=='-')
  37. {
  38. str[i]=0;
  39. break;
  40. }
  41. strcpy(str2,str+i+1);
  42. }
  43. int main ()
  44. {
  45. while (~scanf("%d",&n))
  46. {
  47. int i;
  48. mp.clear();
  49. for (i=0;i<n;i++)
  50. {
  51. cin>>data[i].name;
  52. mp[ data[i].name ]=i;
  53. }
  54. int a,b,id1,id2;
  55. for (i=1;i<=n*(n-1)/2;i++)
  56. {
  57. In ();
  58. scanf("%d:%d",&a,&b);
  59. id1=mp[str];
  60. id2=mp[str2];
  61. data[id1].score+=a;
  62. data[id1].dif+=a-b;
  63. data[id2].score+=b;
  64. data[id2].dif+=b-a;
  65. if (a>b)
  66. data[id1].num+=3;
  67. else if (a<b)
  68. data[id2].num+=3;
  69. else if (a==b)
  70. data[id1].num+=1,data[id2].num+=1;
  71. }
  72. sort(data,data+n);
  73. sort(data,data+n/2,cmp);
  74. for (i=0;i<n/2;i++)
  75. cout<<data[i].name<<endl;
  76. }
  77. return 0;
  78. }

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读