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

📅  最后修改于: 2021-05-04 12:04:59             🧑  作者: 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


输出:
6054546548