找错误——下面的程序意图在于统计字符串中字符数1的个数,可惜有瑕疵

#include<stdio.h>
#define maxn 10000000+10
int main(){
 char s[maxn];
 scanf("%s",s);
 int tot=0;
 for(int i=0;i<strlen(s);i++)
   if (s[i]==1)tot++;
 printf("%d ",tot);
}

改程序至少有3个问题,一个导致程序无法运行,另一个导致结果不正确,还有一个导致效率低下。你能找到并改正他们吗?

关于此题我只找到了3处错误

(1)缺少#include<string.h>

(2)#define maxn 10000000+10,太大,不能作为数组的大小定义。

(3)if (s[i]==1)tot++;应该为if (s[i]==‘1’)tot++;

此为《算法竞赛入门经典》P55的题目

原文地址:https://www.cnblogs.com/ssfzmfy/p/4262307.html