给定一个整数数组,任务是找出是否有可能使用这些数字的所有数字构造一个整数,使其可以被3整除。如果可能,则打印“是”,如果不可能,则打印“否”。
null
例如:
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.
# Python program to find if it is possible # to make a number divisible by 3 using # all digits of given array def isPossibleToMakeDivisible(arr, n): # Find remainder of sum when divided by 3 remainder = 0 for i in range ( 0 , n): remainder = (remainder + arr[i]) % 3 # Return true if remainder is 0. return (remainder = = 0 ) # main() arr = [ 40 , 50 , 90 ]; n = 3 if (isPossibleToMakeDivisible(arr, n)): print ( "Yes" ) else : print ( "No" ) # Code Contributed by Mohit Gupta_OMG <(0_o)> |
输出:
Yes
时间复杂度:O(n)
请参阅完整的文章 可以使用数组中的所有数字将一个数整除为3 更多细节!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END