预读在数组predict后来用C成果 [英] Read ahead in an array to predict later outcomes in C
问题描述
基本上我想要问的是,反正是有数组中的领先阅读,让你可以为它创建一个案例。
Basically what I'm trying to ask is, is there anyway to read ahead in an array so that you could create a 'case' for it.
例如:您阵列只有整数,如:0 0 0 0 4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 3
For ex: you array has only integers such as: 0 0 0 0 4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 3
和你想尝试做的是创造一个coutdown,直到下一个非零数字。基本上显示倒计时。有没有办法做到这一点?
and what you want to try to do is create a coutdown till the next non zero number. Basically display the countdown. Is there any way to do this?
推荐答案
这code:
#include <stdio.h>
int main(void)
{
int array[] = { 0, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, };
int a_size = sizeof(array) / sizeof(array[0]);
int i = 0;
while (i < a_size)
{
if (array[i] == 0)
{
int j;
for (j = i; j < a_size; j++)
if (array[j] != 0)
break;
printf("%d\n", j - i);
i = j;
}
else
i++;
}
return 0;
}
产生这样的输出:
produces this output:
4
1
12
如果这就是你想要的东西,那是大概你所需要的。如果不是你想要的,你需要更清楚地解释它是什么,你想要的。
If that's what you want, that's roughly what you need. If it is not what you want, you need to explain more clearly what it is that you want.
修订code为修正后的预计产量:
Revised code for revised expected output:
#include <stdio.h>
int main(void)
{
int array[] = { 0, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, };
int a_size = sizeof(array) / sizeof(array[0]);
int i = 0;
while (i < a_size)
{
if (array[i] == 0)
{
int j;
for (j = i; j < a_size; j++)
if (array[j] != 0)
break;
int k = j - i;
while (k > 0)
printf(" %d", k--);
i = j;
}
else
{
printf(" '");
i++;
}
}
putchar('\n');
return 0;
}
修订输出:
4 3 2 1 ' 1 ' 12 11 10 9 8 7 6 5 4 3 2 1 '
这篇关于预读在数组predict后来用C成果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!