洛谷【1042】乒乓球

题目链接:https://www.luogu.org/problemnew/show/P1042
这道题目刚开始自己是卡在了数据的输入和如何把二维的字符矩阵转化为一维的字符串。

#include <iostream>
#include <string.h>

using namespace std;

const int MAX_N = 25 * 2500 + 100;

char str_one[25 + 5], str[MAX_N];

int main () {
	int i, w;
	while (cin >> str_one) {
		int len = strlen (str_one);
		for (i = 0; i < len; i++) {
			if (str_one[i] == 'E') {
				int sum_one = 0, sum_two = 0, j = 0;
				for (j; j < w; j++) {
					if (str[j] == 'W') sum_one ++;
					if (str[j] == 'L') sum_two ++;
					if ((sum_one >= 11 && sum_one - sum_two >= 2) || (sum_two >= 11 &&sum_two - sum_one >= 2)) {
						cout << sum_one << ":" << sum_two << endl;
						sum_one = 0; sum_two = 0;
					} 
				}
				cout << sum_one << ":" << sum_two << endl << endl;
				sum_one = 0; sum_two = 0;
				for (j = 0; j < w; j++) {
					if (str[j] == 'W') sum_one ++;
					if (str[j] == 'L') sum_two ++;
					if ((sum_one >= 21 && sum_one - sum_two >= 2) || (sum_two >= 21 &&sum_two - sum_one >= 2)) {
						cout << sum_one << ":" << sum_two << endl;
						sum_one = 0; sum_two = 0;
					} 
				}
				cout << sum_one << ":" << sum_two << endl;
				return 0;
			}
			else str[w++] = str_one[i];
		}
	}
	return 0;
} 
### 关于洛谷 P1042 乒乓球问题的解题思路 对于该问题,核心在于模拟两个不同计分制度下的比赛过程。给定一系列字符作为输入,其中`W`代表华华得分,`L`代表平平得分,而`E`则表示当前局结束并进入下一局。 #### 输入处理逻辑 程序需逐个读取字符直至遇到`E`为止,在此期间依据所遇字符更新对应选手分数,并根据不同赛制规则判定是否某一方赢得了一局以及何时应开启新的一局[^2]。 #### 不同赛制下的胜负条件 - **11分制**:当任意一名玩家达到11分且领先对手至少两分时即获胜;若双方均达10分以上但差距不足两分,则继续游戏直到满足上述胜利条件。 - **21分制**:同样遵循先得规定分数(这里是21)且超出对方不少于两分者胜出的原则,只是起始目标分数更高一些[^3]。 ### C语言代码实现 下面给出一段完整的C语言源码用于解决这个问题: ```c #include <stdio.h> #define MAXN 1005 char game[MAXN]; int score_11[2], score_21[2]; void reset_scores() { score_11[0] = score_11[1] = 0; score_21[0] = score_21[1] = 0; } // 判断是否在一场比赛中达到了可以赢的状态 bool can_win(int *score, int target) { return (score[0]>=target || score[1]>=target) && abs(score[0]-score[1])>=2; } int main(){ while(scanf("%s",game)!=EOF){ bool is_end=false; char winner=' '; reset_scores(); for(int i=0; !is_end&&game[i]!='\0'; ++i){ switch(game[i]){ case 'W': ++score_11[0]; ++score_21[0]; break; case 'L': ++score_11[1]; ++score_21[1]; break; case 'E': if(can_win(score_11, 11)){ printf("11:%c %d-%d\n",(winner=(score_11[0]>score_11[1])?'H':'P'),score_11[0],score_11[1]); reset_scores(); // 准备下一个回合 } if(can_win(score_21, 21)){ printf("21:%c %d-%d\n", (winner=(score_21[0]>score_21[1])?'H':'P'),score_21[0],score_21[1]); reset_scores(); // 准备下一个回合 } is_end=true; break; } if(!is_end){ // 如果不是最后一轮打印中间状态 printf("11:"); if(winner==' ')printf("%d-%d\n",score_11[0],score_11[1]); else printf("-%d-%d\n",score_11[0],score_11[1]); printf("21:"); if(winner==' ')printf("%d-%d\n",score_21[0],score_21[1]); else printf("-%d-%d\n",score_21[0],score_21[1]); } } } } ``` 这段代码实现了对每一轮比赛中两位参赛者的得分情况进行记录,并按照两种不同的记分方式分别计算最终成绩。每当检测到一局结束符`E`时,会检查是否有选手符合各自赛制下的获胜标准,并据此输出相应结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值