geeksforgeeks@ Largest Number formed from an Array
Posted 流白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了geeksforgeeks@ Largest Number formed from an Array相关的知识,希望对你有一定的参考价值。
http://www.practice.geeksforgeeks.org/problem-page.php?pid=380
Largest Number formed from an Array
Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.
The result is going to be very large, hence return the result in the form of a string.
Input:
The first line of input consists number of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, and the second line has the elements of the array.
Output:
In each separate line print the largest number formed by arranging the elements of the array in the form of a string.
Constraints:
1 ≤ T ≤ 70
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 1000
Example:
Input:
2
5
3 30 34 5 9
4
54 546 548 60
Output:
9534330
6054854654
import java.util.*; import java.lang.*; import java.io.*; class Entry { public String str; public Entry(String s) { super(); this.str = s; } } class cmp implements Comparator<Entry> { public int compare(Entry e1, Entry e2) { String s1 = e1.str; String s2 = e2.str; StringBuffer combo1 = new StringBuffer(); StringBuffer combo2 = new StringBuffer(); combo1.append(s1); combo1.append(s2); combo2.append(s2); combo2.append(s1); return (combo2.toString()).compareTo(combo1.toString()); } } class GFG { public static String func(int[] arr) { int n = arr.length; ArrayList<Entry> ls = new ArrayList<Entry> (); for(int i=0; i<n; ++i) { Entry entry = new Entry(Integer.toString(arr[i])); ls.add(entry); } Collections.sort(ls, new cmp()); StringBuffer sb = new StringBuffer(); for(Entry entry: ls) { sb.append(entry.str); } return sb.toString(); } public static void main (String[] args) { Scanner in = new Scanner(System.in); int times = in.nextInt(); for(int i=0; i<times; ++i) { int n = in.nextInt(); int[] arr = new int[n]; for(int j=0; j<n; ++j) { arr[j] = in.nextInt(); } System.out.println(func(arr)); } } }
以上是关于geeksforgeeks@ Largest Number formed from an Array的主要内容,如果未能解决你的问题,请参考以下文章
为啥这段代码在 leetcode 运行良好,但在 geeksforgeeks 出现分段错误?
geeksforgeeks@ Equal to product (Binary Search)