通过根据给定条件将每个元素替换为其数字的总和或乘积来修改给定数组
给定一个由N个整数组成的数组arr[] ,任务是在对每个数组元素执行以下操作之一后修改数组元素:
- 如果数组元素中偶数位数大于奇数位数,则将该元素更新为该元素所有位数的总和。
- 否则,将该元素更新为该元素所有数字的乘积。
例子:
Input: arr[] = {113, 141, 214, 3186}
Output: 3 4 7 3186
Explanation:
Following are the operation performed on each array elements:
- For element arr[0](= 113): count of even and odd digits are 0 and 3. As count of even < count of odd digit, therefore update arr[0](= 113) to the product of each digit of the number 113 i.e., 1 * 1 * 3 = 3.
- For element arr[1](= 141): count of even and odd digits are 1 and 2. As count of even < count of odd digit, therefore update arr[1](= 141) to the product of each digit of the number 141 i.e., 1 * 4 * 1 = 4.
- For element arr[2]:(= 214) count of even and odd digits are 2 and 1. As count of even > count of odd digit, therefore update arr[2](= 214) to the sum of each digit of the number 214 i.e., 2 + 1 + 4 = 7.
- For element arr[3](= 3186): count of even and odd digits are 2 and 2. As count of even is the same as the count of odd digit, then no operation is performed. Therefore, arr[3](= 3186) remains the same.
After the above operations, the array modifies to {3, 4, 7, 3186}.
Input: arr[] = {2, 7, 12, 22, 110}
Output: 2 7 12 4 0
方法:给定的问题可以通过对每个数组元素执行给定的操作并相应地打印结果来解决。请按照以下步骤解决问题:
- 遍历给定的数组arr[]并执行以下步骤:
- 查找数组当前元素的偶数和奇数位数。
- 如果偶数和奇数的计数相同,则不需要执行任何操作。
- 如果数组元素中偶数位数大于奇数位数,则将该元素更新为该元素所有位数的总和。
- 否则,将该元素更新为该元素所有数字的乘积。
- 完成上述步骤后,将数组arr[]打印为修改后的数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to modify the given array
// as per the given conditions
void evenOdd(int arr[], int N)
{
// Traverse the given array arr[]
for (int i = 0; i < N; i++) {
// Initialize the count of even
// and odd digits
int even_digits = 0;
int odd_digits = 0;
// Initialize temp with the
// current array element
int temp = arr[i];
// For count the number of
// even digits
while (temp) {
// Increment the odd count
if ((temp % 10) & 1)
odd_digits++;
// Otherwise
else
even_digits++;
// Divide temp by 10
temp /= 10;
}
// Performe addition
if (even_digits > odd_digits) {
int res = 0;
while (arr[i]) {
res += arr[i] % 10;
arr[i] /= 10;
}
cout << res << " ";
}
// Performe multiplication
else if (odd_digits > even_digits) {
int res = 1;
while (arr[i]) {
res *= arr[i] % 10;
arr[i] /= 10;
}
cout << res << " ";
}
// Otherwise
else
cout << arr[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 113, 141, 214, 3186 };
int N = sizeof(arr) / sizeof(arr[0]);
evenOdd(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to modify the given array
// as per the given conditions
static void evenOdd(int[] arr, int N)
{
// Traverse the given array arr[]
for (int i = 0; i < N; i++) {
// Initialize the count of even
// and odd digits
int even_digits = 0;
int odd_digits = 0;
// Initialize temp with the
// current array element
int temp = arr[i];
// For count the number of
// even digits
while (temp > 0) {
// Increment the odd count
if ((temp % 10) % 2 != 0)
odd_digits++;
// Otherwise
else
even_digits++;
// Divide temp by 10
temp /= 10;
}
// Performe addition
if (even_digits > odd_digits) {
int res = 0;
while (arr[i] > 0) {
res += arr[i] % 10;
arr[i] /= 10;
}
System.out.print(res + " ");
}
// Performe multiplication
else if (odd_digits > even_digits) {
int res = 1;
while (arr[i] > 0) {
res *= arr[i] % 10;
arr[i] /= 10;
}
System.out.print(res + " ");
}
// Otherwise
else
System.out.print(arr[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 113, 141, 214, 3186 };
int N = arr.length;
evenOdd(arr, N);
}
}
// This code is contributed by rishavmahato348.
Python3
# Python program for the above approach
# Function to modify the given array
# as per the given conditions
def evenOdd(arr,N):
# Traverse the given array arr[]
for i in range(N):
# Initialize the count of even
# and odd digits
even_digits = 0;
odd_digits = 0;
# Initialize temp with the
# current array element
temp = arr[i];
# For count the number of
# even digits
while (temp):
# Increment the odd count
if ((temp % 10) & 1):
odd_digits += 1;
# Otherwise
else:
even_digits += 1;
# Divide temp by 10
temp = temp//10
# Performe addition
if (even_digits > odd_digits):
res = 0;
while (arr[i]):
res += arr[i] % 10;
arr[i] = arr[i]//10;
print(res, end=" ");
# Performe multiplication
elif (odd_digits > even_digits):
res = 1;
while (arr[i]):
res *= arr[i] % 10;
arr[i] = arr[i]//10
print(res, end=" ");
# Otherwise
else:
print(arr[i], end=" ");
# Driver Code
arr = [113, 141, 214, 3186 ];
N = len(arr);
evenOdd(arr, N);
# This code is contributed by _saurabh_jaiswal
C#
// C# program for the above approach
using System;
class GFG {
// Function to modify the given array
// as per the given conditions
static void evenOdd(int[] arr, int N)
{
// Traverse the given array arr[]
for (int i = 0; i < N; i++) {
// Initialize the count of even
// and odd digits
int even_digits = 0;
int odd_digits = 0;
// Initialize temp with the
// current array element
int temp = arr[i];
// For count the number of
// even digits
while (temp > 0) {
// Increment the odd count
if ((temp % 10) % 2 != 0)
odd_digits++;
// Otherwise
else
even_digits++;
// Divide temp by 10
temp /= 10;
}
// Performe addition
if (even_digits > odd_digits) {
int res = 0;
while (arr[i] > 0) {
res += arr[i] % 10;
arr[i] /= 10;
}
Console.Write(res + " ");
}
// Performe multiplication
else if (odd_digits > even_digits) {
int res = 1;
while (arr[i] > 0) {
res *= arr[i] % 10;
arr[i] /= 10;
}
Console.Write(res + " ");
}
// Otherwise
else
Console.Write(arr[i] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 113, 141, 214, 3186 };
int N = arr.Length;
evenOdd(arr, N);
}
}
// This code is contributed by subham348.
Javascript
输出:
3 4 7 3186
时间复杂度: O(N)
辅助空间: O(1)