给定以字符串表示的数字N,如果数字之和为偶数且可被4整除,或者数字的总和为奇数且可被3整除,则任务是打印“是”。否则为“否”。
例子:
Input: 12345
Output: Yes
Input: 894561
Output: Yes
以下是逐步算法:
- 计算所有数字的总和。
- 如果总和是偶数:
- 检查总和是否可被4整除
- 否则,如果总和是奇数:
- 检查是否可以被3整除。
- 如果步骤2或步骤3中的任何一种情况都满足,则打印“是”,否则打印“否”。
C++
// C++ implementation of above algorithm
#include
using namespace std;
// Function to check the sum
bool checkSum(string num)
{
int sum = 0;
// Traverse each digit
for (int i = 0; i < num.length(); i++) {
// converting a character to integer by
// taking difference of their ASCII value
int digit = num[i] - '0';
sum += digit;
}
// Check if sum is even and divisible by 4
// or if sum is odd and divisible by 3 then
// return true, else return false
if ((sum % 2 == 0 && sum % 4 == 0)
|| (sum % 2 != 0 && sum % 3 == 0))
return true;
return false;
}
// Driver code
int main()
{
string num = "12347";
checkSum(num) ? cout << "Yes" : cout << "No";
return 0;
}
Java
// Java implementation of above algorithm
import java.lang.*;
class Geeks {
// Function to check the sum
static boolean checkSum(String num)
{
int sum = 0;
// Traverse each digit
for (int i = 0; i < num.length(); i++)
{
// converting a character to integer by
// taking difference of their ASCII value
int digit = num.charAt(i) - '0';
sum += digit;
}
// Check if sum is even and divisible by 4
// or if sum is odd and divisible by 3 then
// return true, else return false
if ((sum % 2 == 0 && sum % 4 == 0) ||
(sum % 2 !=0 && sum % 3 == 0))
return true;
return false;
}
// Driver code
public static void main(String args[])
{
String num = "12347";
System.out.println(checkSum(num) ? "Yes" : "No");
}
}
// This code is contributed by ankita_saini.
Python 3
# Python 3 implementation of
# above algorithm
# Function to check the sum
def checkSum(num):
sum = 0
# Traverse each digit
for i in range(len(num)):
# converting a character to
# integer by taking difference
# of their ASCII value
digit = ord(num[i]) - ord('0')
sum += digit
# Check if sum is even and
# divisible by 4 or if sum
# is odd and divisible by 3
# then return true, else
# return false
if ((sum % 2 == 0 and sum % 4 == 0) or
(sum % 2 != 0 and sum % 3 == 0)):
return True
return False
# Driver code
if __name__ == "__main__":
num = "12347"
print("Yes") if checkSum(num) else print("No")
# This code is contributed
# by ChitraNayal
C#
// C# implementation of above algorithm
using System;
class GFG
{
// Function to check the sum
static bool checkSum(String num)
{
int sum = 0;
// Traverse each digit
for (int i = 0; i < num.Length; i++)
{
// converting a character to
// integer by taking difference
// of their ASCII value
int digit = num[i] - '0';
sum += digit;
}
// Check if sum is even and
// divisible by 4 or if sum
// is odd and divisible by 3
// then return true, else
// return false
if ((sum % 2 == 0 && sum % 4 == 0) ||
(sum % 2 !=0 && sum % 3 == 0))
return true;
return false;
}
// Driver code
public static void Main(String []args)
{
String num = "12347";
Console.WriteLine(checkSum(num) ?
"Yes" : "No");
}
}
// This code is contributed
// by ankita_saini.
PHP
Javascript
Python3
# Python implementation of above approach
def getResult(n):
# Converting integer to string
st = str(n)
# Initialising sum to 0
sum = 0
length = len(st)
# Traversing through the string
for i in st:
# Converting character to int
sum = sum + int(i)
if ((sum % 2 == 0 and sum % 4 == 0) or
(sum % 2 != 0 and sum % 3 == 0)):
return 'Yes'
return 'No'
# Driver Code
n = 202
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus
输出:
No
时间复杂度: O(N)
方法2:使用字符串:
- 我们必须通过采用新变量将给定数字转换为字符串。
- 遍历字符串,将每个元素转换为整数并将其加和。
- 如果总和是偶数,请检查总和是否可被4整除
- 否则,如果总和是奇数,请检查它是否可以被3整除。
- 如果步骤3或步骤4中的任何一种情况都满足,则打印“是”,否则打印“否”。
下面是上述方法的实现:
Python3
# Python implementation of above approach
def getResult(n):
# Converting integer to string
st = str(n)
# Initialising sum to 0
sum = 0
length = len(st)
# Traversing through the string
for i in st:
# Converting character to int
sum = sum + int(i)
if ((sum % 2 == 0 and sum % 4 == 0) or
(sum % 2 != 0 and sum % 3 == 0)):
return 'Yes'
return 'No'
# Driver Code
n = 202
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus
输出:
Yes
时间复杂度: O(N)
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。