题解 P1042 【乒乓球】

· · 题解

!!! 其实这道题有一种简便的算法,可以设一个自定义函数,把分制输进去就可以出答案。这样可以少写一写不必要的代码。(我才不会告诉你我是懒) 贴代码:

#include <iostream>
using namespace std;
string s="";

bool scu(int a,int b,int x)
{
    if(a>=x&&a-b>=2||b>=x&&b-a>=2)  
      return true;
    return false;
} 
void bifen(int x)
{
    int i;
    int a=0,b=0;
    for(i=0;i<s.size();i++)
    {
        if(s[i]=='W')  a++;
        else  b++;
        if(scu(a,b,x))
        {
            cout<<a<<":"<<b<<endl;
            a=b=0;
        }
    }
    cout<<a<<":"<<b<<endl;
}
int main()
{
    string t;
    int w;
    while(cin>>t)
      s+=t;
    w=s.find('E',0);
    s=s.substr(0,w);
    bifen(11);
    cout<<endl;
    bifen(21); 
    return 0;
}

(^_^)