Recover the Smallest Number (30)
来源:互联网 发布:淘宝男装牛仔衣服 编辑:程序博客网 时间:2024/05/21 11:23
题意:
把几组数据组合起来起来找其中最小的
解答:
这里用到一个结论,其实要使得连接起来的数,最小我们把a和b连接起来,如果ab(连接)比ba小,则应该先a后b,所以问题就变成一个排序问题了
所以我们可以先进行排序,写一个比较器,先进行排序,然后组合,删除前面的0项
java的String操作的有坐标的一般都是前闭后开,比如说delete(1,2),删除1保留2
具体java代码如下:
import java.util.*;public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int N=in.nextInt(); ArrayList<String> list=new ArrayList(); for(int i=0;i<N;i++){ String str=in.next(); list.add(str); } Collections.sort(list,new Comparator<String>(){ public int compare(String s1,String s2){ if((s1+s2).compareTo(s2+s1)>0){ return 1; } else if((s1+s2).compareTo(s2+s1)<0) return -1; else return 0; } }); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < list.size(); i++) { buffer.append(list.get(i)); } while (buffer.length() > 0 && buffer.charAt(0) == '0') { buffer.delete(0, 1); } if (buffer.length() == 0) { buffer.append(0); } System.out.print(buffer.toString()); } }
0 0
- Recover the Smallest Number (30)
- Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)-PAT
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- PAT1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)
- PAT 1038. Recover the Smallest Number (30)
- java数据结构 拓扑排序
- 函数声明、函数表达式、匿名函数
- 杨辉三角
- 第一个java程序
- 路由事件
- Recover the Smallest Number (30)
- 最大最小公倍数
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Programming in Objective-C 学习笔记09——对象复制与归档
- 简单程序循环练习
- SQL 优化
- [BZOJ 2938][Poi2000]病毒
- iOSUI简单的页面轮转
- 架构师的行为准则(一)