编写C程序.(英文题目)T is an array from 1 to N integers.At the beginning,each cell has been initialized to 0,that means that the cell is free.Dec is an integer.We want to refill the array T following this way:\x05- T[0] ← 1\x05- Pass Dec

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/18 22:35:06
编写C程序.(英文题目)Tisanarrayfrom1toNintegers.Atthebeginning,eachcellhasbeeninitializedto0,thatmeansthatthe

编写C程序.(英文题目)T is an array from 1 to N integers.At the beginning,each cell has been initialized to 0,that means that the cell is free.Dec is an integer.We want to refill the array T following this way:\x05- T[0] ← 1\x05- Pass Dec
编写C程序.(英文题目)
T is an array from 1 to N integers.At the beginning,each cell has been initialized to 0,that means that the cell is free.Dec is an integer.
We want to refill the array T following this way:
\x05- T[0] ← 1
\x05- Pass Dec empty cells and put the value 2;
\x05- From this last cell,pass Dec empty cells and put 3.When a cell is already full (not free),go on until finding an empty cell.When we are at the end of the array,restart from the beginning.
\x05- Stop when you have put N in a cell,so when the array is full.

编写C程序.(英文题目)T is an array from 1 to N integers.At the beginning,each cell has been initialized to 0,that means that the cell is free.Dec is an integer.We want to refill the array T following this way:\x05- T[0] ← 1\x05- Pass Dec
其中N和DEC的数值来源,你可以改成从用户输入
另外,pass这个意思略有不明,比如我这里写的是3,那究竟是跳过3个,在第4个位置上填写值,还是跳过2个,再第3个位置上填写值.我这里实现的是后者
#include
#define N 10
#define DEC 3
int main() {
int array[N], i, j, k;
for (i = 0; i != N; ++i)
array[i] = 0;
j = 0;
array[j] = 1;
for (i = 1; i != N; ++i) {
k = DEC;
while (k > 0) {
j = (j + 1) % N;
if (array[j] == 0)
--k;
}
array[j] = i + 1;
}
for (i = 0; i != N; ++i)
printf("%d ", array[i]);
return 0;
}