给定一个由小写字符组成的字符串str ,任务是根据给定的规则更改该字符串后,检查该字符串是否可被6整除:
- ‘a’更改为1 。
- ‘b’更改为2 。
- …
- ‘z’更改为26 。
例如,字符串“ abz”将变为1226 。
例子:
Input: str = “ab”
Output: Yes
“ab” is equivalent to 12 which is divisible by 6.
Input: str = “abc”
Output: No
123 is not divisible by 6.
方法:可以通过一个简单的数学技巧来解决:仅当数字的所有位数之和可被3整除且该数字的最后一位被2整除时,该数字才能被6整除。查找所形成数字的数字总和,并将其存储在可变总和中。另外,找到数字的最后一位并将其存储在lastDigit中。
现在,如果总和可被3整除,而lastDigit可被2整除,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the sum
// of the digits of n
int sumDigits(int n)
{
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
// Function that return true if the
// decoded string is divisible by 6
bool isDivBySix(string str, int n)
{
// To store the sum of the digits
int sum = 0;
// For each character, get the
// sum of the digits
for (int i = 0; i < n; i++) {
sum += (int)(str[i] - 'a' + 1);
}
// If the sum of digits is
// not divisible by 3
if (sum % 3 != 0)
return false;
// Get the last digit of
// the number formed
int lastDigit = ((int)(str[n - 1]
- 'a' + 1))
% 10;
// If the last digit is
// not divisible by 2
if (lastDigit % 2 != 0)
return false;
return true;
}
// Driver code
int main()
{
string str = "ab";
int n = str.length();
if (isDivBySix(str, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the sum
// of the digits of n
static int sumDigits(int n)
{
int sum = 0;
while (n > 0)
{
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
// Function that return true if the
// decoded string is divisible by 6
static boolean isDivBySix(String str, int n)
{
// To store the sum of the digits
int sum = 0;
// For each character, get the
// sum of the digits
for (int i = 0; i < n; i++)
{
sum += (int)(str.charAt(i) - 'a' + 1);
}
// If the sum of digits is
// not divisible by 3
if (sum % 3 != 0)
return false;
// Get the last digit of
// the number formed
int lastDigit = ((int)(str.charAt(n - 1) -
'a' + 1)) % 10;
// If the last digit is
// not divisible by 2
if (lastDigit % 2 != 0)
return false;
return true;
}
// Driver code
public static void main(String []args)
{
String str = "ab";
int n = str.length();
if (isDivBySix(str, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to return the sum
# of the digits of n
def sumDigits(n) :
sum = 0;
while (n > 0) :
digit = n % 10;
sum += digit;
n //= 10;
return sum;
# Function that return true if the
# decoded string is divisible by 6
def isDivBySix(string , n) :
# To store the sum of the digits
sum = 0;
# For each character, get the
# sum of the digits
for i in range(n) :
sum += (ord(string[i]) -
ord('a') + 1);
# If the sum of digits is
# not divisible by 3
if (sum % 3 != 0) :
return False;
# Get the last digit of
# the number formed
lastDigit = (ord(string[n - 1]) -
ord('a') + 1) % 10;
# If the last digit is
# not divisible by 2
if (lastDigit % 2 != 0) :
return False;
return True;
# Driver code
if __name__ == "__main__" :
string = "ab";
n = len(string);
if (isDivBySix(string, n)) :
print("Yes");
else :
print("No");
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the sum
// of the digits of n
static int sumDigits(int n)
{
int sum = 0;
while (n > 0)
{
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
// Function that return true if the
// decoded string is divisible by 6
static bool isDivBySix(String str, int n)
{
// To store the sum of the digits
int sum = 0;
// For each character, get the
// sum of the digits
for (int i = 0; i < n; i++)
{
sum += (int)(str[i] - 'a' + 1);
}
// If the sum of digits is
// not divisible by 3
if (sum % 3 != 0)
return false;
// Get the last digit of
// the number formed
int lastDigit = ((int)(str[n - 1] -
'a' + 1)) % 10;
// If the last digit is
// not divisible by 2
if (lastDigit % 2 != 0)
return false;
return true;
}
// Driver code
public static void Main(String []args)
{
String str = "ab";
int n = str.Length;
if (isDivBySix(str, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by 29AjayKumar
输出:
Yes