📌  相关文章
📜  通过连接数组旋转中的每个元素的最大数量

📅  最后修改于: 2021-10-25 11:26:50             🧑  作者: Mango

给定一个包含 N 个元素的数组。任务是通过连接每个旋转中的每个元素来打印最大数量。在每次旋转中,第一个元素将取代每个旋转中的最后一个元素,反之亦然。
例子:

处理方法:仔细观察,发现所有元素中最左边一位数最大的数将是该数中的第一个元素。由于串联必须根据数组的旋转来完成。连接从最大的最左边数字索引到末尾的所有数字,然后连接从0索引到最大最左边数字索引的元素。
下面是上述方法的实现:

C++
// C++ program to print the
// Maximum number by concatenating
// every element in rotation of array
#include 
using namespace std;
 
// Function to print the largest number
void printLargest(int a[], int n)
{
 
    // store the index of largest
    // left most digit of elements
    int max = -1;
    int ind = -1;
 
    // Iterate for all numbers
    for (int i = 0; i < n; i++) {
 
        int num = a[i];
 
        // check for the last digit
        while (num) {
            int r = num % 10;
            num = num / 10;
            if (num == 0) {
                // check for the largest left most digit
                if (max < r) {
                    max = r;
                    ind = i;
                }
            }
        }
    }
 
    // print the largest number
 
    // print the rotation of array
    for (int i = ind; i < n; i++)
        cout << a[i];
 
    // print the rotation of array
    for (int i = 0; i < ind; i++)
        cout << a[i];
}
 
// Driver Code
int main()
{
    int a[] = { 54, 546, 548, 60 };
    int n = sizeof(a) / sizeof(a[0]);
    printLargest(a, n);
    return 0;
}


Java
// Java program to print the
// Maximum number by concatenating
// every element in rotation of array
import java.util.*;
import java.lang.*;
 
public class GFG {
    // Function to print the largest number
    static void printLargest(int a[], int n)
    {
        // store the index of largest
        // left most digit of elements
        int max = -1;
        int ind = -1;
 
        // Iterate for all numbers
        for (int i = 0; i < n; i++) {
            int num = a[i];
 
            // check for the last digit
            while (num > 0) {
                int r = num % 10;
                num = num / 10;
                if (num == 0) {
                    // check for the largest left most digit
                    if (max < r) {
                        max = r;
                        ind = i;
                    }
                }
            }
        }
        // print the largest number
 
        // print the rotation of array
        for (int i = ind; i < n; i++)
            System.out.print(a[i]);
 
        // print the rotation of array
        for (int i = 0; i < ind; i++)
            System.out.print(a[i]);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int a[] = { 54, 546, 548, 60 };
        int n = a.length;
        printLargest(a, n);
    }
}


Python3
# Python program to print the
# Maximum number by concatenating
# every element in rotation of array
  
# Function to print the largest number
def printLargest(a, n):
 
      
    # store the index of largest
    # left most digit of elements
    max =-1
    ind =-1
      
    # Iterate for all numbers
    for i in range(0, n):
         num = a[i]
          
        # check for the last digit
         while(num):
         
            r = num % 10;
            num = num / 10;
            if(num == 0):
                # check for the largest left most digit
                if(max


C#
// C# program to print the
// Maximum number by concatenating
// every element in rotation of array
using System;
 
class GFG {
 
    // Function to print the largest number
    static void printLargest(int[] a, int n)
    {
        // store the index of largest
        // left most digit of elements
        int max = -1;
        int ind = -1;
 
        // Iterate for all numbers
        for (int i = 0; i < n; i++) {
            int num = a[i];
 
            // check for the last digit
            while (num > 0) {
                int r = num % 10;
                num = num / 10;
                if (num == 0) {
                    // check for the largest left most digit
                    if (max < r) {
                        max = r;
                        ind = i;
                    }
                }
            }
        }
        // print the largest number
 
        // print the rotation of array
        for (int i = ind; i < n; i++)
            Console.Write(a[i]);
 
        // print the rotation of array
        for (int i = 0; i < ind; i++)
            Console.Write(a[i]);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] a = { 54, 546, 548, 60 };
        int n = 4;
        printLargest(a, n);
    }
}
 
// This code is contributed by mohit kumar 29


PHP


Javascript


输出:
6054546548

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程