2018 US corporations face questions - the smallest number of no-show

Today, the United States and Communist Youth League wrote a trick question, it is a tragedy, front too slow, the last time the code is not enough, thought out how to do it, then just ended was thought out, Behind his! ! !

Did not remember the title, probably gives a string array, the array of numbers which is '0' - '9', if it is '0', '1', '2', '3', '4', these the number can be composed for example 1,2,3,4,10,11,12,13,14,20, ............ 12340,43210 such numbers. So now we find out the smallest integer not appear, such as 1,2. Here no-show, the first number is 3, such as 0123456789, a no-show, the first digit is 11.

So now let's build an array used to calculate the number of times each character appears, such 000111223456687999.

Then the corresponding number of array which is {3,3,2,1,1,1,2,1,1,3} we first find out the smallest number first, as this is the subscript 3 a. Under labeled 4,5,7,8 though is 1, but not the first.

Here we divided into two kinds:

First: the smallest number is 0 we get the index corresponding to the two cases would then.

(1) are greater than the other number corresponding to the number marked 0. Such as { 2 , 13,12,11,11,11,12,11,11,13}, it can be directly output 1000 (corresponding to the back of the subscript 1 with a plurality of more than 0 0)

(1) has as many. Such as { 2 , 13, 2 , 11,11,11,12,11,11,13}, since only the most significant bit is non-zero, it can take a first 2, then the size of each element is: { 2 , 13, 1 , 11,11,11,12,11,11,13}. We then take the next directly 2, not to take continuous, then take the one that is the result. Who is this time together other than 0 is the smallest, it will have a continuous output it wants. Specific operation is the number corresponding to 0 + 1, and then find the minimum.

Second: the smallest number except we get index 0 corresponding to the other.

Direct access to the index, this index is assigned to continuously take data Ci + 1 wants.

code show as below:

import java.util.*;
import java.util.Scanner;


public class Main {
public static int get(char [] a,char b)
{ int i=0;
for( i=0;i<10;i++)
if(b==a[i])
break;
return i;
}
public static int get(int [] a) {
int min=0;
int temp=a[0];
for (int i = 1; i <10; i++ )  
        if (a[i] < temp)  
        { min =i; 
        temp=a[i];
        }
    return min;  
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(input.hasNext())
{
String str=input.nextLine();
String str1="0123456789";
char [] strArr = str.toCharArray();
char [] strArr1 = str1.toCharArray();
int [] sum=new int [10];
int index=0;
for(int i=0;i<strArr.length;i++)
{
  sum[get(strArr1,strArr[i])]++;
}
index=get(sum);
if(index==0)
{
sum[0]++;
index=get(sum);
if(index==0)
{System.out.print(1);
for(int i=0;i<=sum[index];i++)
{
System.out.print(0);
}
}
else 
for(int i=0;i<=sum[index];i++)
{
System.out.print(index);
}
}
else
{
sum[index]--;
index=get(sum);
if(index==0)
{
System.out.print(1);
for(int i=0;i<=sum[index];i++)
{
System.out.print(0);
}
}
else 
for(int i=0;i<=sum[index]+1;i++)
{
System.out.print(index);
}
}
}
}
}

If you have any questions where I did not think or have a better way. Please advise grateful! ! !

Thank you for criticism! ! !

Guess you like

Origin blog.csdn.net/mad_sword/article/details/79660594