📌  相关文章
📜  反转每个元素后数组元素的总和

📅  最后修改于: 2021-09-06 06:49:12             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是在反转每个数组元素的数字后找到所有数组元素的总和。

例子:

方法:思想是按照给定条件对给定数组的每个数字进行反转,并找到反转后形成的所有数组元素的总和。解决问题的步骤如下:

  1. 初始化一个变量,比如sum ,以存储所需的总和。
  2. 将变量count初始化为0并将f初始化为false以存储arr[i]的结尾0的计数和标志以避免所有非结尾0
  3. rev初始化为0以存储每个数组元素的反转。
  4. 遍历给定数组并对每个数组元素执行以下操作:
    • 递增计数并将arr[i]除以 10,直到遍历完末尾的所有零。
    • f重置为 true,这意味着已考虑所有结尾的 0 数字。
    • 现在,通过更新rev = rev*10 + arr[i] % 10arr[i] = arr[i]/10 来反转arr[i]
    • 在遍历 arr[i] 的每一位后,更新rev = rev * Math.pow(10, count)以将所有结尾的 0 添加到反向数字的末尾。
  5. 对于上述步骤中的每个反向数字,将该值添加到结果sum 中

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sum of elements
// after reversing each element in arr[]
void totalSum(int arr[], int n)
{
     
    // Stores the final sum
    int sum = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
    {
         
        // Stores count of ending 0s
        int count = 0;
 
        int rev = 0, num = arr[i];
 
        // Flag to avoid count of 0s
        // that doesn't ends with 0s
        bool f = false;
 
        while (num > 0)
        {
             
            // Count of ending 0s
            while (num > 0 && !f &&
                   num % 10 == 0)
            {
                count++;
                num = num / 10;
            }
 
            // Update flag with true
            f = true;
 
            // Reversing the num
            if (num > 0)
            {
                rev = rev * 10 +
                      num % 10;
 
                num = num / 10;
            }
        }
 
        // Add all ending 0s to
        // end of rev
        if (count > 0)
            rev = rev * pow(10, count);
 
        // Update sum
        sum = sum + rev;
    }
 
    // Print total sum
    cout << sum;
}
 
// Driver Code
int main()
{
     
    // Given arr[]
    int arr[] = { 7, 234, 58100 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    totalSum(arr, n);
   
    return 0;
}
 
// This code is contributed by akhilsaini


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find the sum of elements
    // after reversing each element in arr[]
    static void totalSum(int[] arr)
    {
        // Stores the final sum
        int sum = 0;
 
        // Traverse the given array
        for (int i = 0;
            i < arr.length; i++) {
 
            // Stores count of ending 0s
            int count = 0;
 
            int rev = 0, num = arr[i];
 
            // Flag to avoid count of 0s
            // that doesn't ends with 0s
            boolean f = false;
 
            while (num > 0) {
 
                // Count of ending 0s
                while (num > 0 && !f
                    && num % 10 == 0) {
                    count++;
                    num = num / 10;
                }
 
                // Update flag with true
                f = true;
 
                // Reversing the num
                if (num > 0) {
                    rev = rev * 10
                        + num % 10;
 
                    num = num / 10;
                }
            }
 
            // Add all ending 0s to
            // end of rev
            if (count > 0)
                rev = rev
                    * (int)Math.pow(10,
                                    count);
 
            // Update sum
            sum = sum + rev;
        }
 
        // Print total sum
        System.out.print(sum);
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        // Given arr[]
        int[] arr = { 7, 234, 58100 };
 
        // Function Call
        totalSum(arr);
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the sum of elements
# after reversing each element in arr[]
def totalSum(arr, n):
     
    # Stores the final sum
    sums = 0
 
    # Traverse the given array
    for i in range(0, n):
         
        # Stores count of ending 0s
        count = 0
 
        rev = 0
        num = arr[i]
 
        # Flag to avoid count of 0s
        # that doesn't ends with 0s
        f = False
 
        while num > 0:
 
            # Count of ending 0s
            while (num > 0 and f == False and
                   num % 10 == 0):
                count = count + 1
                num = num // 10
 
            # Update flag with true
            f = True
 
            # Reversing the num
            if num > 0:
                rev = rev * 10 + num % 10
                num = num // 10
 
        # Add all ending 0s to
        # end of rev
        if (count > 0):
            rev = rev * pow(10, count)
 
            # Update sum
        sums = sums + rev
 
    # Print total sum
    print(sums)
 
# Driver Code
if __name__ == "__main__":
 
    # Given arr[]
    arr = [ 7, 234, 58100 ]
 
    n = len(arr)
 
    # Function call
    totalSum(arr, n)
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the sum of elements
// after reversing each element in arr[]
static void totalSum(int[] arr)
{
     
    // Stores the final sum
    int sum = 0;
 
    // Traverse the given array
    for(int i = 0; i < arr.Length; i++)
    {
         
        // Stores count of ending 0s
        int count = 0;
 
        int rev = 0, num = arr[i];
 
        // Flag to avoid count of 0s
        // that doesn't ends with 0s
        bool f = false;
 
        while (num > 0)
        {
             
            // Count of ending 0s
            while (num > 0 && !f &&
                   num % 10 == 0)
            {
                count++;
                num = num / 10;
            }
 
            // Update flag with true
            f = true;
 
            // Reversing the num
            if (num > 0)
            {
                rev = rev * 10 +
                      num % 10;
 
                num = num / 10;
            }
        }
 
        // Add all ending 0s to
        // end of rev
        if (count > 0)
            rev = rev * (int)Math.Pow(10, count);
 
        // Update sum
        sum = sum + rev;
    }
 
    // Print total sum
    Console.Write(sum);
}
 
// Driver Code
static public void Main()
{
 
    // Given arr[]
    int[] arr = { 7, 234, 58100 };
 
    // Function call
    totalSum(arr);
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
18939

时间复杂度: O(N*log 10 M),其中 N 表示数组的长度,M 表示最大数组元素。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live