在这个问题中,我们得到了n个数字和一个数字的列表。我们的任务是查找列表中的每个数字是否都可将其整除。
我们需要检查给定的数字是否划分了列表的所有元素。
输入: list [] = [4,10,6,5,9] num = 5
输出: 否
解释:
元素4、6、9不能被5整除。
解决方法:
为了解决这个问题,我们需要检查列表中的任何元素是否可以被num整除。如果列表的每个数量都可以被num整除,则返回true,否则返回false。
算法:
步骤1: 为i循环-> 0到n,n是列表的长度。
步骤1.1: 如果list [i]%num!= 0,则返回-1。
步骤1.2: 否则,执行list [i]%num == 0,继续。
步骤2: 返回1。
#include <iostream>
using namespace std;
bool isListDivNum(int list[], int num, int size)
{
for (int i = 0; i < size; i++) {
if (list[i] % num != 0)
return false;
}
return true;
}
int main() {
int list[] = {762, 9, 123, 99};
int num = 3;
int size = (sizeof(list) / sizeof(list[0]));
if (isListDivNum(list, num , size))
cout<<"All elements of the list are divisible by number";
else
cout<<"All elements of the list are not divisible by number";
return 0;
}All elements of the list are divisible by number