LegendPei @ 2024-10-07 22:30:52
#include <stdio.h>
#include<math.h>
int main()
{
int str, numbers[100000] = { 0 }, i = 0;
while (1)
{
str = getchar();
if (str == '\n')continue;
if (str == 'W')numbers[i] = 1;
if (str == 'L')numbers[i] = 2;
i++;
if (str == 'E')break;
}
int w = 0, l = 0;
for (int j = 0; 1; j++)
{
if (numbers[j] == 1)w++;
if (numbers[j] == 2)l++;
if ((abs(w - l) >= 2 ) && (w >= 11 || l >= 11))
{
printf("%d:%d\n", w, l);
w = 0, l = 0;
}
if (numbers[j] == 0)
{
printf("%d:%d\n\n", w, l);
break;
}
}
w = 0, l = 0;
for (int j = 0; 1; j++)
{
if (numbers[j] == 1)w++;
if (numbers[j] == 2)l++;
if ((abs(w - l) >= 2 ) && (w >= 21 || l >= 21))
{
printf("%d:%d\n", w, l);
w = 0, l = 0;
}
if (numbers[j] == 0)
{
printf("%d:%d", w, l);
break;
}
}
return 0;
}
by Martin_L @ 2024-10-08 20:38:49
这题的问题在于,万一它的换行不是\n,你的代码就行不通了;因此,建议用 if else-if else 的结构排除。
完整代码:
#include <stdio.h>
#include<math.h>
int main()
{
int str, numbers[100000] = { 0 }, i = 0;
while (1)
{
str = getchar();
if (str == 'E')break;
if (str == 'W')numbers[i] = 1;
else if (str == 'L')numbers[i] = 2;
else continue;
i++;
}
int w = 0, l = 0;
for (int j = 0; 1; j++)
{
if (numbers[j] == 1)w++;
if (numbers[j] == 2)l++;
if ((abs(w - l) >= 2 ) && (w >= 11 || l >= 11))
{
printf("%d:%d\n", w, l);
w = 0, l = 0;
}
if (numbers[j] == 0)
{
printf("%d:%d\n\n", w, l);
break;
}
}
w = 0, l = 0;
for (int j = 0; 1; j++)
{
if (numbers[j] == 1)w++;
if (numbers[j] == 2)l++;
if ((abs(w - l) >= 2 ) && (w >= 21 || l >= 21))
{
printf("%d:%d\n", w, l);
w = 0, l = 0;
}
if (numbers[j] == 0)
{
printf("%d:%d", w, l);
break;
}
}
return 0;
}
by Bennyboy2024 @ 2024-10-08 22:39:27
看了下大佬的代码。我刚上过一个夏令营,天赋不行。学的很差。不敢用getchar(), 等。
看了下,这里吧str 改 char 类型,然后 #include <iostream> 最后把 getchar() 改 std::cin 就可以了。
#include <stdio.h>
#include<math.h>
#include <iostream> //用下大佬不用的cin
int main()
{
int numbers[100000] = { 0 }, i = 0;
char str; // int 改成 char
while (1)
{
std::cin >> str; // getchar() 变 cin 就 AC 了
if (str == '\n')continue;
if (str == 'W')numbers[i] = 1;
if (str == 'L')numbers[i] = 2;
i++;
if (str == 'E')break;
}