乒乓球
2025-08-13 09:50:23
发布于:北京
正解1:
#include<bits/stdc++.h>
using namespace std;
char C;
string S;
int n,A,B;
void Work(int Lim)
{
for(char i:S)
{
if(i=='W')A++;
if(i=='L')B++;
if(max(A,B)>=Lim&&abs(A-B)>=2)
{
cout<<A<<":"<<B<<endl;
A=0,B=0;
}
}
printf("%d:%d\n\n",A,B);
A=B=0;
}
int main()
{
while(cin>>C)
{
if(C=='E')break;
S+=C;
}
Work(11),Work(21);
return 0;
}
正解2:
#include<bits/stdc++.h>
using namespace std;
char C;
string S;
int n,A,B;
int main()
{
while(cin>>C)
{
if(C=='E')break;
S+=C;
}
for(char i:S)
{
if(i=='W')A++;
if(i=='L')B++;
if(max(A,B)>=11&&abs(A-B)>=2)
{
cout<<A<<":"<<B<<endl;
A=0,B=0;
}
}
printf("%d:%d\n",A,B);
A=B=0;
puts("");
for(char i:S)
{
if(i=='W')A++;
if(i=='L')B++;
if(max(A,B)>=21&&abs(A-B)>=2)
{
cout<<A<<":"<<B<<endl;
A=0,B=0;
}
}
printf("%d:%d\n",A,B);
return 0;
}
错解:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
void processGame(const string& records, int winScore) {
int hua = 0, opp = 0;
for (char c : records) {
if (c == 'W') hua++;
else if (c == 'L') opp++;
if ((hua >= winScore || opp >= winScore) && abs(hua - opp) >= 2) {
cout << hua << ":" << opp << endl;
hua = opp = 0;
}
}
// 输出未完成的局
if (hua != 0 || opp != 0) {
cout << hua << ":" << opp << endl;
}
}
int main() {
string input, records;
while (getline(cin, input)) {
for (char c : input) {
if (c == 'E') goto end_input;
if (c == 'W' || c == 'L') records += c;
}
}
end_input:
// 处理11分制
processGame(records, 11);
cout << endl;
// 处理21分制
processGame(records, 21);
return 0;
}
这里空空如也
有帮助,赞一个