我想创建一个代码,其中给定的数字在1到10之间的有序数组,代码返回所有缺少的值。查找数组中缺失的数字
我的代码如下:
int missingArray [] = {1, 3, 4, 5, 7, 8};
for (int i = 0; i < 11; i++) {
if (missingArray[i] == i+1) {
cout << "Continue. \n";
}
if (missingArray[i] != i+1) {
cout << "The value of " << i+1 << " is missing. \n";
}
}
我想要的代码返回 继续 2的值丢失 继续 继续 继续 6的值丢失 继续 继续 9的值缺失 缺少值10
但是相反,在我得到第一个“缺失”元素后,它列出了所有缺失的内容。任何人有任何建议?
您可能还想查看[Find missing number](http://stackoverflow.com/questions/3492302/easy-interview-question-got-harder-given-numbers-1-100-find-the-missing -numbe)线程在Stackoverflow – WhiZTiM
在调试器中逐步执行程序。当我是2时,你会注意到一些事情。 –