给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:
给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558。
现给定数字,请编写程序输出能够组成的最小的数。
每个输入包含1个测试用例。每个测试用例在一行中给出10个非负整数,顺序表示我们拥有数字0、数字1、……数字9的个数。整数间用一个空
格分隔。10个数字的总个数不超过50,且至少拥有1个非0的数字。
在一行中输出能够组成的最小的数。
2 2 0 0 0 3 0 0 1 0
10015558
public class T1013 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//只读取一次,减少I/O次数
String[] input = sc.nextLine().split(" ");
//每个数字的次数
int[] counts = new int[10];
for (int i = 0; i < input.length; i++){
counts[i] = Integer.parseInt(input[i]);
}
//处理首位数字,如果次数不为0则退出循环
int firstNonZero = 1;
while (firstNonZero < counts.length && counts[firstNonZero] == 0){
firstNonZero++;
}
//拼接首位数
StringBuilder builder = new StringBuilder();
if (firstNonZero < counts.length){
builder.append(firstNonZero);
counts[firstNonZero]--;
}
//拼接剩余数
for (int i = 0; i < counts.length; i++){
while (counts[i] != 0){
builder.append(i);
counts[i]--;
}
}
System.out.println(builder.toString());
}
} import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a[] = new int[10]; String x = ""; for (int i = 0; i < 10; i++) { a[i] = sc.nextInt(); } if (a[0] == 0) { for (int i = 1; i < 10; i++) { while (a[i] != 0) { x = x + i; a[i]--; } } } else { for(int i=1;i<10;i++) { if(a[i]!=0) { x=x+i; a[i]--; break; } } for(int i=0;i<10;i++) { while(a[i]!=0) { x=x+i; a[i]--; } } } System.out.print(x); } }
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
//创建键盘输入
Scanner scan = new Scanner(System.in);
//定义数组接收键盘录入值,以下标进行区分
int[] a = new int[10];
//定义字符串用于加和并输出
String sum = "";
//定义计数器在第2个位置添加一个@
int t = 1;
//定义s对0进行存储
String s = "";
for (int i = 0; i < 10; i++) {
a[i] = scan.nextInt();
}
scan.close();
if(a[0] != 0) {
for (int i = 0; i < a[0]; i++) {
s += 0;
}
}
//向字符串进行加和
for (int i = 1; i < 10; i++) {
for (int j = 0; j < a[i]; j++, t++) {
if(t == 1) {
sum += i + "@";
}else {
sum += i;
}
}
} //利用正则表达式进行替换
System.out.println(sum.replaceAll("\\W", s));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int []nn=new int[10];
for(int i=0;i<10;i++) {
nn[i]=in.nextInt();
}
String s="";
for(int i=0;i<10;i++) {
for(int j=0;j<nn[i];j++) {
s+=i;
}
}
char[]mm=s.toCharArray();
int []pp=new int[mm.length];
for(int i=0;i<mm.length;i++) {
pp[i]=mm[i]-'0';
}
Arrays.sort(pp);
int min=10;
for(int i=0;i<pp.length;i++) {
if(pp[i]<min&&pp[i]!=0) {
min=pp[i];
pp[i]=20;
}
}
String p=""+min;
for(int i=0;i<pp.length;i++) {
if(pp[i]<10) {
p+=pp[i];
}
}
System.out.println(p);
}
}
//把所有0都插入第二个位置上,其他按顺序输出就可以了
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
ArrayList<Integer> al = new ArrayList<>();
int[] nums = new int[10];
for (int i = 0; i < 10; i++) {
nums[i] = sc.nextInt();
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < nums[i]; j++) {
al.add(i);
}
}
System.out.print(al.get(0));
for (int i = 0; i < nums[0]; i++) {
System.out.print(0);
}
for (int i = 1; i < al.size(); i++) {
System.out.print(al.get(i));
}
System.out.println();
}
}
}
可以AC,可以A
import java.util.ArrayList;
import java.util.Scanner;
public class Main { @SuppressWarnings("resource") public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] intArray = new int[10];// 这里面的10个数字,代表0-9分别有多少个 for (int i = 0; i < intArray.length; i++) { intArray[i] = in.nextInt(); } ArrayList<Integer> arrayList = new ArrayList<Integer>();// 建立一个集合,存放所有的数字 int i = 0;// 代表现在数到第几个数字上去了 while (i < 10) { for (int j = 0; j < intArray[i]; j++) { arrayList.add(i);// 只要在这个循环内,就给我往上加 } i++; } /* * for (Integer integer : arrayList) { System.out.println(integer); } */ Object[] array = arrayList.toArray(); for (int j = 0; j < array.length; j++) { if (array[j] != array[0]&&Integer.valueOf(array[0].toString())==0) { Object t = array[j]; array[j] = array[0]; array[0] = t; break; } } for (int j = 0; j < array.length; j++) { System.out.print(array[j]); } }
}
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new LinkedList<Integer>();
int get = 0;
for(int i = 0;i<10;i++){
get = sc.nextInt();
for(int j=0;j<get;j++){
list.add(i);
}
}
Collections.sort(list);
String tmp ="";
boolean first = true;
if(! list.contains(0)){
for(int i=0;i<list.size();i++){
System.out.print(list.get(i));
}
}else{
for(int i=0;i<list.size();i++){
if(list.get(i)==0){
tmp+=list.get(i);
}else if(list.get(i)!=0 && first){
System.out.print(list.get(i));
System.out.print(tmp);
first = false;
}else{
System.out.print(list.get(i));
}
}
}
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] nums = new int[10];
for(int i=0;i<nums.length;i++){
nums[i] = sc.nextInt();
}
String str ="";
for(int i= 1 ;i<nums.length;i++){
if(nums[i]>0){
str= str+i;
nums[i]--;
break;
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]>0){
for(int j=1 ; j<=nums[i];j++){
str=str+i;
}
}
}
System.out.println(str);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num[] = new int[10];
for(int i=0;i<10;i++){
num[i] = in.nextInt();
}
int c[] = new int[50];
int count=0;
String str = "";
for(int i=0;i<10;i++){
for(int j=0;j<num[i];j++){
c[count] = i;
count++;
}
}
int temp = c[0];
c[0] = c[num[0]];
c[num[0]] = temp;
for(int i=0;i<count;i++){
str = str + c[i];
}
System.out.println(str);
}
}