#include
int seglen(int *a, int size, int key);
int main(void) {
int a[] = {3, 2, 1, 0, 0, 0, 0, 3, 4, 2, 5, 1, 0, 0, 1};
int b[] = {3, 2, 1, 3, 3, 3, 0, 3, 4, 2, 5, 1, 0, 0, 1};
int i = 0;
printf("array:");
for(i = 0; i < 15; i++) printf(" %d", a[i]);
printf("\nlongest length of `0' segments: %d\n--\n", seglen(a, sizeof(a), 0));
printf("array:");
for(i = 0; i < 15; i++) printf(" %d", b[i]);
printf("\nlongest length of `3' segments: %d\n--\n", seglen(b, sizeof(b), 3));
printf("array:");
for(i = 0; i < 15; i++) printf(" %d", a[i]);
printf("\nlongest length of `8' segments: %d\n--\n", seglen(a, sizeof(a), 8));
return 0;
}
int seglen(int *a, int size, int key) {
int i = 0;
int x = 0;
int retval = 0;
while(i < size) {
if(a[i] != key)
x = i + 1;
retval = (retval > (i + 1 - x)) ? retval : (i + 1 - x);
i++;
}
return retval;
}
int seglen(int *a, int size, int key);
int main(void) {
int a[] = {3, 2, 1, 0, 0, 0, 0, 3, 4, 2, 5, 1, 0, 0, 1};
int b[] = {3, 2, 1, 3, 3, 3, 0, 3, 4, 2, 5, 1, 0, 0, 1};
int i = 0;
printf("array:");
for(i = 0; i < 15; i++) printf(" %d", a[i]);
printf("\nlongest length of `0' segments: %d\n--\n", seglen(a, sizeof(a), 0));
printf("array:");
for(i = 0; i < 15; i++) printf(" %d", b[i]);
printf("\nlongest length of `3' segments: %d\n--\n", seglen(b, sizeof(b), 3));
printf("array:");
for(i = 0; i < 15; i++) printf(" %d", a[i]);
printf("\nlongest length of `8' segments: %d\n--\n", seglen(a, sizeof(a), 8));
return 0;
}
int seglen(int *a, int size, int key) {
int i = 0;
int x = 0;
int retval = 0;
while(i < size) {
if(a[i] != key)
x = i + 1;
retval = (retval > (i + 1 - x)) ? retval : (i + 1 - x);
i++;
}
return retval;
}
No comments:
Post a Comment