【发布时间】:2018-11-03 21:35:07
【问题描述】:
首先,此代码(PenaltyShootout.c)用于计算给定字符串中以 2 开头的 1 的数量。 “0” - 没有目标, “1” - 目标, “2” - 犯规。
问题:PenatltyShootout.exe 停止工作。
#include <stdio.h>
#include <string.h>
int main()
{
int T,i;
char str[100][500];
int n=0;
没有。的测试用例是没有的。输入不同的字符串以检查代码的工作。
do
{
printf("Enter the number of Test cases(must be between 1 and 100):\n");
scanf("%d",&T);
}while(T>100);
我尝试将 str[i][500] 替换为 (char *) str[i][500] 并且警告消失了,但 PenaltyShootout.exe 停止工作。
for(i=0; i<T; i++)
{
printf("Enter the test case %d\n",i);
scanf("%s",str[i][500]);
}
for(i=0; i<T; i++)
{
for(int j=0; j<strlen(str[i])-1; j++)
{
if((str[i][j]=='2')&&(str[i][j+1]=='1')==1)
{
n++;
}
}
这应该打印犯规后的进球数。
printf("%d\n",n);
}
return 0;
}
【问题讨论】:
-
scanf("%s",str[i]) -
"尝试用
(char *) str[i][500]替换str[i][500],警告消失了,但 PenaltyShootout.exe 停止工作。" -- 永远不要尝试抛弃错误——发生了非常糟糕的事情......