洛谷P1042乒乓球(模拟,技巧细心)

题目链接:https://www.luogu.org/problemnew/show/P1042

题目简单,就是模拟。但有两点需要注意:

1.输入方法,用while(cin),用错输入方法一直错。

2.到11或21分不一定结束!还要满足分差>=2,不然继续比下去!

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iomanip>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 using namespace std;
 9 typedef long long ll;
10 typedef unsigned long long ull;
11 const int maxn=1e6*8;
12 const int inf=1e9;
13 //char a[maxn];
14 string a;
15 int main()
16 {
17     ios::sync_with_stdio(false); cin.tie(0);
18 
19     /*char c;
20     int p=0;
21     while((c=getchar())!='E')
22     {
23         if(c=='
') continue;
24         a[p++]=c;
25     }
26     int len=strlen(a);*///输入问题,一直错误
27 
28     char c;
29     while(cin>>c && c!='E') a+=c;
30     int len=a.length();
31     int p1=0,p2=0;
32     for(int i=0;i<=len-1;i++)
33     {
34         if(a[i]=='W') p1++;
35         else p2++;
36 
37         if((p1>=11 || p2>=11) && abs(p1-p2)>=2)
38         {
39             cout<<p1<<':'<<p2<<endl;
40             p1=0;
41             p2=0;
42         }
43     }
44     cout<<p1<<':'<<p2<<endl;
45 
46     cout<<endl;
47 
48     p1=0;p2=0;
49     for(int i=0;i<=len-1;i++)
50     {
51         if(a[i]=='W') p1++;
52         else p2++;
53 
54         if((p1>=21 || p2>=21) && abs(p1-p2)>=2)
55         {
56             cout<<p1<<':'<<p2<<endl;
57             p1=0;
58             p2=0;
59         }
60     }
61     cout<<p1<<':'<<p2<<endl;
62 
63     return 0;
64 }

完。

原文地址:https://www.cnblogs.com/redblackk/p/9738845.html