正整数去重取最大值
来源:互联网 发布:神行者定位软件 编辑:程序博客网 时间:2024/06/07 13:31
import java.util.*;/*正整数去重之后取最大数423234-->4324423-->423*/public class Mian2 { public static void main(String[] args) { Scanner fin = new Scanner(System.in); // while (fin.hasNext()) { String str = fin.nextLine(); // if (str.charAt(0) == '-') { // System.out.println(0); // break; // } Set<Character> set = new TreeSet<Character>(); for (int i = 0; i < str.length(); i++) { set.add(str.charAt(i)); } char[] c = new char[set.size()]; Iterator<Character> it = set.iterator(); int index = 0; while (it.hasNext()) { c[index] = it.next(); index++; } List<Integer> list = new ArrayList<Integer>(); permutation(c, 0, c.length - 1, list); System.out.println(list); // Object[] array = list.toArray(); Arrays.sort(array); List<String> list1 = new ArrayList<String>(); zuhe(str, list1, c.length); for (int i = array.length - 1; i >= 0; i--) { if (list1.contains(array[i].toString())) { System.out.println(array[i]); break; } } // } } // 字符组合 public static void zuhe(String str, List<String> list, int length) { int count = (int) (Math.pow(2, str.length()) - 1);// 7 for (int i = 1; i <= count; i++) { String ss = Integer.toBinaryString(i).toString(); while (ss.length() < str.length()) { ss = "0" + ss; } StringBuffer sb = new StringBuffer(); for (int j = 0; j < ss.length(); j++) { if (ss.charAt(j) == '1') { sb.append(str.charAt(j)); } } if (sb.length() == length) { list.add(sb.toString()); } // System.out.print(sb + " "); } } // 字符排列 public static void permutation(char[] s, int from, int to, List<Integer> list) { if (to <= 1) return; if (from == to) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length; i++) { sb.append(s[i]); } list.add(Integer.parseInt(sb.toString())); } else { for (int i = from; i <= to; i++) { swap(s, i, from); // 交换前缀,使其产生下一个前缀 permutation(s, from + 1, to, list); swap(s, from, i); // 将前缀换回,继续做上一个前缀的排列 } } } public static void swap(char[] s, int i, int j) { char tmp = s[i]; s[i] = s[j]; s[j] = tmp; }}
阅读全文
0 0
- 正整数去重取最大值
- 四个正整数选最大值
- 求四个正整数的最大值
- 给定四个正整数,求出最大值
- Week7-2三个正整数的最大值
- 给定四个正整数,求出四个数的最大值并输出
- 给定四个正整数,求出四个数的最大值和最小值
- 第七周 项目三 求三个正整数的最大值
- 输入任意正整数a,b,c,返回最大值
- 输入不定长正整数,任意组合,输出最大值。
- 给定一个正整数数组求组合起来的最大值
- 把正整数n表示成若干个正整数的和,求积的最大值
- 把正整数n表示成若干个不同的正整数的和,求积的最大值
- 正整数
- POJ - 1032 Parliament( 把正整数n表示成若干个不同的正整数的和,求积的最大值)
- 求二叉树中结点的最大值(所有结点的值都是正整数)
- 198.House Robber (求非相邻正整数的和最大值)
- Java 随机生成50个正整数,存放到数组,求出总和,平均值,最大值,最小值
- 数据结构实验之查找五:平方之哈希表
- Hibernate的cascade属性 配置效果。
- 解决centos7中python-pip模块不存在的问题
- Laravel中使用GroupBy时产生的错误MySQL : isn't in GROUP BY
- 每天一道算法题——LeeCode.389. Find the Difference
- 正整数去重取最大值
- 2264 Card Game (First Edition)
- API接口安全性设计
- __int64 与long long 的区别, C/C++的64位整型 不同编译器间的比较
- 四种动画
- 内核函数前面单下划线和双下划线
- HDU6164(计算几何--点关于直线的对称点)
- [Leetcode] 370. Range Addition 解题报告
- 系统API接口规范