如何为循环中的数组设置特定值?-C
我正在学习一门初级编程课程,我们的任务是制作一个以10人开始的游戏程序。你从第一个人开始,数到三,然后放下那个人。你一直这样做直到只剩下一个人如何为循环中的数组设置特定值?-C,c,arrays,sorting,C,Arrays,Sorting,我正在学习一门初级编程课程,我们的任务是制作一个以10人开始的游戏程序。你从第一个人开始,数到三,然后放下那个人。你一直这样做直到只剩下一个人 #include <stdio.h> #include <stdlib.h> int main() { int n[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, i = 0; int * ptrN = &n[i]; for( i = 0; n[i]; n[i+3])
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, i = 0;
int * ptrN = &n[i];
for( i = 0; n[i]; n[i+3]){
if (n[i+3] = 1){
*ptrN = 0;
}
else if (n[i] = 0)
continue;
else
printf("Wrong");
}
printf("The value of n0 %d\tn1 %d\tn2 %d\tn3 %d\tn4 %d\tn5 %d\tn6 %d\tn7 %d\tn8 %d\tn9 %d", n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[8], n[9]);
return 0;
}
#包括
#包括
int main()
{
int n[10]={1,1,1,1,1,1,1,1,1},i=0;
int*ptrN=&n[i];
对于(i=0;n[i];n[i+3]){
如果(n[i+3]=1{
*ptrN=0;
}
else如果(n[i]=0)
继续;
其他的
printf(“错误”);
}
printf(“n0%d\tn1%d\tn2%d\tn3%d\tn4%d\tn5%d\tn6%d\tn7%d\tn8%d\tn9%d”,n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],n[9]);
返回0;
}
我现在离我的答案还很远,但我已经遇到了问题。当我运行上述代码时,它只会更改n[0]的值,然后退出程序。希望您能给予指导。感谢您如评论中所述:
=
进行比较,=
用于赋值n[i]
i
0
1
时,您还需要停止
解决这个问题的方法不止一种,但你可以通过设置一个计数器来计算有多少个1
,并设置一个while
-循环,当计数器降到1
时结束:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, count = 10;
/* `i` needs to start at -1 so that we start counting:
* "0, 1, 2. n[2] is out!"
* instead of counting:
* "1, 2, 3. n[3] is out!"
*/
int i = -1;
while (count > 1) {
/* We need to count three of those persons that are not out.
* That means we have to search for the next person that is not
* out (by skipping over `0`) and repeat that three times.
*
* If we just increase `i` by 3 ( `(i + 3) % 10` ) then we are
* not checking how many of those persons between `n[i]` and
* `n[(i + 3) % 10]` needed to be skipped over.
*
* So repeat three times:
*/
for (int j = 0; j < 3; j++) {
/* This will search for the next person that is not out by
* increasing `i` by one until `n[i]` is not `0`:
*/
do {
i = (i + 1) % 10;
} while (n[i] == 0); // Check next person if this one is out.
} // I forgot to close this bracket in my first version of the answer
n[i] = 0;
count--;
}
printf("The value of n0 %d\tn1 %d\tn2 %d\tn3 %d\tn4 %d\tn5 %d\tn6 %d\tn7 %d\tn8 %d\tn9 %d", n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[8], n[9]);
return 0;
}
#包括
#包括
int main()
{
int n[10]={1,1,1,1,1,1,1,1,1},count=10;
/*`i`需要从-1开始计算:
*“0,1,2。n[2]已退出!”
*而不是计算:
*“1,2,3.n[3]出局了!”
*/
int i=-1;
而(计数>1){
/*我们需要数一数那些没有被排除在外的人中的三个。
*这意味着我们必须寻找下一个不是
*跳出(跳过“0”)并重复三次。
*
*如果我们只是将'i'增加3('i+3)%10'),那么我们就是
*不检查其中有多少人介于'n[i]`和'
*`n[(i+3)%10]`需要跳过。
*
*所以重复三次:
*/
对于(int j=0;j<3;j++){
/*这将搜索下一个不在的人
*将'i'增加一,直到'n[i]`不是'0':
*/
做{
i=(i+1)%10;
}while(n[i]==0);//检查下一个人是否已退出。
}//我忘了在我的第一个答案中关闭这个括号
n[i]=0;
计数--;
}
printf(“n0%d\tn1%d\tn2%d\tn3%d\tn4%d\tn5%d\tn6%d\tn7%d\tn8%d\tn9%d”,n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],n[9]);
返回0;
}
噢,我试图在删除该人员后将n[]的值设置为0。一旦只剩下1“1”,我如何设置循环的出口?预期的输出是什么?ptrN
始终指向数组的第一个元素。更新i
不会自动更新ptrN
。您需要使用=
进行比较,=
用于分配。如果您先用英语描述程序应该做什么,可能会更好。非常感谢。你能解释一下for循环吗?。我不知道j是从哪里来的。我相信第一个离开的人是n[2]对吗?对于i值,它不需要是i=(i+3)%10;?很抱歉问了这么多问题。D:我会添加更多的注释来更好地解释我的代码。