AcWing 413. 乒乓球
原题链接
简单
算法1
80分代码
#include <bits/stdc++.h>
#define max 100
#include <string>
using namespace std;
bool panduan(string theMatch){
for(int i=0;i<theMatch.length();i++){
if(theMatch[i]=='E'){
return true;
}
}
return false;
}
int main()
{
int theScoreW=0;
int theScoreL=0;
int theScoreW21=0;
int theScoreL21=0;
bool ran=false;
string theMatch;//比赛输入数据
string theResult,theResult21;//输出数据
string strDst,strDst21;
while(!panduan(theMatch)){
string tmp;
cin>>tmp;
theMatch+=tmp;
}
for(int i=0;i<theMatch.length();++i){
if(theMatch[i]=='W'){
++theScoreW;
++theScoreW21;
}else if(theMatch[i]=='L'){
++theScoreL;
++theScoreL21;
}
stringstream ssTemp;
ssTemp<<theScoreW;
ssTemp<<':';
ssTemp<<theScoreL;
strDst=ssTemp.str();
if((theScoreW>=11||theScoreL>=11)&&abs(theScoreL-theScoreW)>=2){
theResult+=strDst+"\n";
theScoreW=0;
theScoreL=0;
ran=true;
}
ssTemp.str("");;
ssTemp<<theScoreW21;
ssTemp<<':';
ssTemp<<theScoreL21;
strDst21=ssTemp.str();
if((theScoreW21>=21||theScoreL21>=21)&&abs(theScoreL21-theScoreW21)>=2){
theResult21+=strDst21+"\n";
theScoreW21=0;
theScoreL21=0;
ran=true;
}
if(theMatch[i]=='E'){
break;
}
}
if((theScoreW||theScoreL)||!ran){
theResult+=strDst+"\n";
}
if(theScoreW21||theScoreL21||!ran){
theResult21+=strDst21;
}else{
theResult21.substr(0,((int)theResult21.length())-2);
}
cout<<theResult<<endl<<theResult21;
return 0;
}
算法2
100分代码
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string s1,s2;
while(getline(cin,s2))s1+=s2;
int i=0,a=0,b=0;
while(s1[i]!='E')
{
switch(s1[i])
{
case 'W':a++;break;
case 'L':b++;break;
}
if((a>=11||b>=11)&&(abs(a-b)>=2))
{
cout<<a<<":"<<b<<endl;
a=0;b=0;
}
i++;
}
cout<<a<<":"<<b<<endl;
cout<<endl;
a=b=0;i=0;
while(s1[i]!='E')
{
switch(s1[i])
{
case 'W':a++;break;
case 'L':b++;break;
}
if((a>=21||b>=21)&&(abs(a-b)>=2))
{
cout<<a<<":"<<b<<endl;
a=0;b=0;
}
i++;
}
cout<<a<<":"<<b<<endl;
return 0;
}