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

📅  最后修改于: 2021-04-22 02:31:09             🧑  作者: Mango

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

例子:

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

  1. 初始化一个变量,例如sum ,以存储所需的和。
  2. 将变量count初始化为0 ,将f初始化为false,以存储arr [i]的结尾0 s的计数,并标记以避免所有非结尾的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. 对于上述步骤中的每个反向数字,将该值添加到结果总和中

下面是上述方法的实现:

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)