如何检查数组的所有值是否等于0?

编程入门 行业动态 更新时间:2024-10-27 02:22:26
本文介绍了如何检查数组的所有值是否等于0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

程序的上下文是一个涉及钉子和光盘的游戏.用户输入桩的数量(最多20个)和每个桩上的碟片的数量(最大10个).假设有足够的碟片可移动,两名玩家每回合在一个钉上来回移动任何数量的碟片.删除最后一张光盘的播放器丢失.

The context of the program is a game involving pegs and discs. The user inputs the amount of pegs (max of 20) and the amount of discs on each peg (max of 10). Two players go back and forth removing any amount of discs on a single peg each turn, given that there are enough discs to remove on that peg. The player to remove the last disc loses.

光盘的数量存储在阵列中,其中阵列的索引对应于桩号.我有一个布尔函数,用于检查钉子是否没有光盘,这表明有人赢了.我的代码中存在一些逻辑错误,但我无法弄清楚它是什么:

The number of discs are stored in an array, where the index of the array corresponds the peg number. I have a boolean function that checks whether or not the pegs are empty of discs, implying someone has won. There is some logical error in my code but I can't figure out what it is:

bool checkPegs(int array[], int size) { int checker(0); for (int i = 0; i < size; i++) { if(array[i] = 0) { return true; } else { return false; } } }

推荐答案

bool checkPegs(int array[], int size) { for (int i = 0; i < size; i++) { if(array[i] != 0) { return false; } } return true; }

更多推荐

如何检查数组的所有值是否等于0?

本文发布于:2023-11-30 00:27:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!