c++ 判断int数组是否包含数字1
可以使用循环遍历整个数组,判断数组中的每一个元素是否等于1。如果找到等于1的元素,返回true;否则,返回false。
以下是一个示例代码:
#include <iostream>
bool containsOne(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] == 1) {
return true;
}
}
return false;
}
int main() {
int arr[] = {2, 3, 4, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);
if (containsOne(arr, size)) {
std::cout << "数组包含数字1" << std::endl;
} else {
std::cout << "数组不包含数字1" << std::endl;
}
return 0;
}
输出结果为:数组包含数字1
原文地址: https://www.cveoy.top/t/topic/i5Wm 著作权归作者所有。请勿转载和采集!