📌  相关文章
📜  C / C++程序,检查是否可以使用数组中的所有数字将其整除为3

📅  最后修改于: 2021-05-28 01:58:11             🧑  作者: Mango

给定一个整数数组,任务是查找是否可以使用这些数字的所有数字来构造一个整数,以使其可以被3整除。如果可能,则打印“是”,否则打印“否” ”。

例子:

Input : arr[] = {40, 50, 90}
Output : Yes
We can construct a number which is
divisible by 3, for example 945000. 
So the answer is Yes. 

Input : arr[] = {1, 4}
Output : No
The only possible numbers are 14 and 41,
but both of them are not divisible by 3, 
so the answer is No.
// C++ program to find if it is possible
// to make a number divisible by 3 using
// all digits of given array
#include 
using namespace std;
  
bool isPossibleToMakeDivisible(int arr[], int n)
{
    // Find remainder of sum when divided by 3
    int remainder = 0;
    for (int i = 0; i < n; i++)
        remainder = (remainder + arr[i]) % 3;
  
    // Return true if remainder is 0.
    return (remainder == 0);
}
  
// Driver code
int main()
{
    int arr[] = { 40, 50, 90 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (isPossibleToMakeDivisible(arr, n))
        printf("Yes\n");
    else
        printf("No\n");
  
    return 0;
}
输出:
Yes

时间复杂度:O(n)

请参阅完整的文章,关于使用数组中的所有数字将数字除以3的可能性,以获取更多详细信息!

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。