#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\n",tot);
}
改程序至少有3个问题,一个导致程序无法运行,另一个导致结果不正确,还有一个导致效率低下。你能找到并改正他们吗?
关于此题我只找到了3处错误
(1)缺少#include<string.h>
(2)#define maxn 10000000+10,太大,不能作为数组的大小定义。
(3)if (s[i]==1)tot++;应该为if (s[i]==‘1’)tot++;
此为《算法竞赛入门经典》P55的题目
 
                    
                     
                    
                 
                    
                 
                
            
         
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号