📌  相关文章
📜  通过根据给定条件将每个元素替换为其数字的总和或乘积来修改给定数组

📅  最后修改于: 2022-05-13 01:56:07.583000             🧑  作者: Mango

通过根据给定条件将每个元素替换为其数字的总和或乘积来修改给定数组

给定一个由N个整数组成的数组arr[] ,任务是在对每个数组元素执行以下操作之一后修改数组元素:

  • 如果数组元素中偶数位数大于奇数位数,则将该元素更新为该元素所有位数的总和。
  • 否则,将该元素更新为该元素所有数字的乘积。

例子:

方法:给定的问题可以通过对每个数组元素执行给定的操作并相应地打印结果来解决。请按照以下步骤解决问题:

  • 遍历给定的数组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)