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的更多相关文章
- [LeetCode179]Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【leetcode】Largest Number
题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode-179. Largest Number
179. Largest Number Given a list of non negative integers, arrange them such that they form the larg ...
- 【leetcode】Largest Number ★
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number -- 数字字符串比较大小
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- 《c程序设计语言》读书笔记--大写转小写
#include <stdio.h> #include <stdlib.h> #include <string.h> int aoti(char c) { if(c ...
- html下select追加元素,IE下错误
var selectCtr=window.document.getElementById("lesson_up"); selectCtr.add(opt,selectCtr.opt ...
- C盘空间不足
C盘空间不足 2014-11-27 Win7实用技巧之七实战C盘空间不足之三招四式
- Java NIO读书笔记
一.Java IO与NIO区别: (1)Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流进行操作的,而NIO ...
- URAL1410. Crack
1410 dp水题 题意读了好一会 是不能连续读两个及以上单词 #include <iostream> #include<cstdio> #include<cstring ...
- Win7平台下Cocos2d-x环境搭建
一.Win7下Cocos2d-x环境搭建 Cocos开发者平台官网——在这里下载游戏引擎 解压放到某个目录下即可 https://www.python.org/downloads/ ...
- 解决eclipse-helios中Errors running builder JavaScript Validator的问题
原文:http://blog.ywxyn.com/index.php/archives/713 解决eclipse-helios中Errors running builder JavaScript V ...
- HDU 1240 (简单三维广搜) Asteroids!
给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很 ...
- 51nod1086 背包问题 V2
我都快不会写二进制优化多重背包了...卡了一下常数从rank100+到20+... #include<cstdio> #include<cstring> #include< ...
- codeforces 450 B Jzzhu and Sequences
题意:给出f1=x,f2=y,f(i)=f(i-1)+f(i+1),求f(n)模上10e9+7 因为 可以求出通项公式:f(i)=f(i-1)-f(i-2) 然后 f1=x; f2=y; f3=y-x ...