最多改变一位数可以被3整除的不同数字的计数
给定一个表示数字的字符串str 有N个数字,任务是通过改变最多一位数来计算使给定数能被 3 整除的方法数。
例子:
Input: str[] = “23”
Output: 7
Explanation: Below are the numbers that can be made from the string which are divisible by 3 – 03, 21, 24, 27, 33, 63, 93
1.Change 2 to 0 (0+3)=3 divisible by 3
2.Change 3 to 1 (2+1)=3 divisible by 3
3 change 3 to 4 (2+4)=6 divisible by 3
4 change 2 to 3 sum is 6 divisible by 3
Similarly there are total 7 number of ways to make the given number divisible by 3
Input: str[] = “235”
Output: 9
方法:解决这个问题的思路很简单。计算给定数字的数字总和,然后对于每个索引,删除该数字并尝试从0到9的所有可能数字,看看总和是否可以被3整除。请按照以下步骤解决问题:
- 将变量sum初始化为0以存储数字的位数之和。
- 使用变量i迭代范围[0, N]并执行以下步骤:
- 在变量sum的第 i 个索引处添加 digit 的值。
- 将变量count初始化为0以存储答案。
- 如果数字本身可以被 3 整除,则将 count 加一。
- 使用变量i迭代范围[0, N]并执行以下步骤:
- 将变量remaining_sum初始化为sum-(number.charAt(i)-48)。
- 使用变量j迭代范围[0, 9]并执行以下步骤:
- 将j的值加到变量remaining_sum中,如果剩余和可被3整除且j不等于第 i 个索引处的数字,则将count的值加1。
- 执行上述步骤后,打印count的值作为答案。
下面是上述方法的实现..
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// possible numbers divisible by 3
void findCount(string number)
{
// Calculate the sum
int sum = 0;
for (int i = 0; i < number.length(); ++i) {
sum += number[i] - 48;
}
// Store the answer
int count = 0;
// Consider the edge case when
// the number itself is divisible by 3
// The count will be added by 1
if (sum % 3 == 0)
count++;
// Iterate over the range
for (int i = 0; i < number.length(); ++i) {
// Decreasing the sum
int remaining_sum = sum - (number[i] - 48);
// Iterate over the range
for (int j = 0; j <= 9; ++j) {
// Checking if the new sum
// is divisible by 3 or not
if ((remaining_sum + j) % 3 == 0
&& j != number[i] - 48) {
// If yes increment
// the value of the count
++count;
}
}
}
cout << count;
}
// Driver Code
int main()
{
// Given number
string number = "235";
findCount(number);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to count the number of
// possible numbers divisible by 3
public static void findCount(String number)
{
// Calculate the sum
int sum = 0;
for (int i = 0; i < number.length(); ++i) {
sum += number.charAt(i) - 48;
}
// Store the answer
int count = 0;
if (sum % 3 == 0)
count++;
// Iterate over the range
for (int i = 0; i < number.length(); ++i) {
// Decreasing the sum
int remaining_sum
= sum - (number.charAt(i) - 48);
// Iterate over the range
for (int j = 0; j <= 9; ++j) {
// Checking if the new sum
// is divisible by 3 or not
if ((remaining_sum + j) % 3 == 0
&& j != number.charAt(i) - 48) {
// If yes increment
// the value of the count
++count;
}
}
}
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
// Given number
String number = "235";
findCount(number);
}
}
Python3
# Python program for the above approach
# Function to count the number of
# possible numbers divisible by 3
def findCount(number):
# Calculate the sum
sum = 0
for i in range(len(number)):
sum += int(number[i]) - 48
# Store the answer
count = 0
if(sum % 3 == 0):
count += 1
# Iterate over the range
for i in range(len(number)):
# Decreasing the sum
remaining_sum = sum - (int(number[i]) - 48)
# Iterate over the range
for j in range(10):
# Checking if the new sum
# is divisible by 3 or not
if ((remaining_sum + j) % 3 == 0 and j != int(number[i]) - 48):
# If yes increment
# the value of the count
count += 1
print(count)
# Driver Code
# Given number
number = "235"
findCount(number)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to count the number of
// possible numbers divisible by 3
static void findCount(string number)
{
// Calculate the sum
int sum = 0;
for (int i = 0; i < number.Length; ++i) {
sum += (int)number[i] - 48;
}
// Store the answer
int count = 0;
if (sum % 3 == 0)
count++;
// Iterate over the range
for (int i = 0; i < number.Length; ++i) {
// Decreasing the sum
int remaining_sum = sum - ((int)number[i] - 48);
// Iterate over the range
for (int j = 0; j <= 9; ++j) {
// Checking if the new sum
// is divisible by 3 or not
if ((remaining_sum + j) % 3 == 0
&& j != number[i] - 48) {
// If yes increment
// the value of the count
++count;
}
}
}
Console.Write(count);
}
// Driver Code
public static void Main()
{
// Given number
string number = "235";
findCount(number);
}
}
Javascript
输出
9
时间复杂度: O(N)
辅助空间: O(1)