紫书(算法竞赛)3.4.2思考题题目2的3个问题

#include <stdio.h>

                                 //隐藏错误:缺少头文件<string.h>

#define maxn 10000000 + 10   //错误1:maxn太大,程序无法运行。
int main()
{
    char s[maxn];
    scanf("%s",s);
    int tot = 0;
    for(int i = 0;i < strlen(s);i++)  //错误3:此处效率低下,应该在for循环外面写"int len = strlen(s);",把里面的换成len。
        if(s[i] == 1) tot++;             //错误2:   1应该加上单引号。此处导致结果不正确。
    printf("%d\n",tot);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhiyeegao/article/details/80100635