There was a storm recently on Jolibi village. The storm was so strong that some trees fell. There are some logs of varied length lying on the ground. The village ground can be represented by a string of length N, where the i-th character is either 1 or 0. A single log is represented by consecutive characters of 1, and two different logs are separated by one or more 0. For example, for the string 1100010111, there are 3 logs. The first one at position 1 to 2 with length 2, the second one at position 6 with length 1, and the third one at position 8 to 10 with length 3. As a carpenter, you want to take one of these logs home. Because you are the senior carpenter, you may take the longest log home. Determine the length of the longest log! Format Input The first line contains an integer N, the length of the string. The next line contains a string of length N, which represents the village ground. Format Output Output an integer X, the length of the longest log. Constraints • 1 ≤ N ≤ 104 • the i-th character is either 0 or 1. • At least one log is present on the given string. Sample Input 1 10 1100010111 Sample Output 1 3 Sample Input 2 10 0110011000 Sample Output 2 2 I have code the question Sir, but the code still not run. Help me to fix this code, Sir, thx. Only use #include and C language Sir. #include int main(){ int size; char log[100]; int longest_log = 0; int logs_count=0; scanf("%d", &size); scanf("%s", &log); while(size>0){ if(log== "1"){ logs_count++; } else{ if(longest_log < logs_count){ longest_log = logs_count; } logs_count = 0; } size--; } printf("%d",longest_log); return 0; }
Log
There was a storm recently on Jolibi village. The storm was so strong that some trees
fell. There are some logs of varied length lying on the ground. The village ground can
be represented by a string of length N, where the i-th character is either 1 or 0. A single
log is represented by consecutive characters of 1, and two different logs are separated by
one or more 0. For example, for the string 1100010111, there are 3 logs. The first one at
position 1 to 2 with length 2, the second one at position 6 with length 1, and the third
one at position 8 to 10 with length 3.
As a carpenter, you want to take one of these logs home. Because you are the senior
carpenter, you may take the longest log home. Determine the length of the longest log!
Format Input
The first line contains an integer N, the length of the string. The next line contains a string of length N, which represents the
village ground.
Format Output
Output an integer X, the length of the longest log.
Constraints
• 1 ≤ N ≤ 104
• the i-th character is either 0 or 1.
• At least one log is present on the given string.
Sample Input 1
10
1100010111
Sample Output 1
3
Sample Input 2
10
0110011000
Sample Output 2
2
I have code the question Sir, but the code still not run. Help me to fix this code, Sir, thx.
Only use #include<stdio.h> and C language Sir.
#include <stdio.h>
int main(){
int size;
char log[100];
int longest_log = 0;
int logs_count=0;
scanf("%d", &size);
scanf("%s", &log);
while(size>0){
if(log== "1"){
logs_count++;
}
else{
if(longest_log < logs_count){
longest_log = logs_count;
}
logs_count = 0;
}
size--;
}
printf("%d",longest_log);
return 0;
}
Step by step
Solved in 4 steps with 4 images