IT company 100-based on the upper row gives 10 number, in its next row to fill out the corresponding 10 numbers

Source: Internet
Author: User

Problem Description:

Give you 10 minutes, according to the upper row give 10 number, in its next row to fill out the corresponding 10 number
The number of rows that are required in the next row is the number of times that the previous 10 numbers appear in the row.
The 10 numbers on the top row are as follows:
"0,1,2,3,4,5,6,7,8,9"

To give an example,
Value: 0,1,2,3,4,5,6,7,8,9
Allocation: 6,2,1,0,0,0,1,0,0,0
0 appeared in the lower row 6 times, 1 in the lower row appeared 2 times,
2 appeared in the next row 1 times, 3 in the lower row appeared 0 times ....
etc...

Problem Analysis:

Its prototype is a bit like the eight queens, all using a retrospective recursive approach to try again until the correct solution is found.

The concrete idea is: constantly go from the bottom row of the array to catch in the corresponding position in the array of the number, if the number is not updated the corresponding values in the row array, only to find the correct value. (The bottom row array is initially an arbitrary value)

Such as:

Upper Row Array a:0,1,2,3,4,5,6,7,8,9
Bottom row Array b:0,1,2,3,4,5,6,7,8,9
Starting with the array index = 0, a[0] = 0,0 The number in the bottom row array is 1, then the next array B is updated to: 1,1,2,3,4,5,6,7,8,9

index = 1, a[1] = 1, 1 in the next row array of the number is 2, then the next array B is updated to: 1,2,2,3,4,5,6,7,8,9, from this constantly down, as long as the correct value can not be found down, if the index >= array length, Then re-restore index = 0 and test down until you find the correct solution.

But this seems to solve only the above-mentioned situation, that is, the number of consecutive N.

Code implementation:

package oschina.mianshi;/** *  @project: oschina *  @filename: it6.java  *  @version: 0.10 *  @author: jm han *  @date: &NBSP;21:46&NBSP;2015/11/2  *  @comment: test purpose *  @result: */import java.util.arraylist; import static tool.util.*;p ublic class it6 {   public static  final int NUM = 10;   ArrayList<Integer> lsta =  New arraylist<integer> ();   arraylist<integer> lstb = new  Arraylist<integer> ();    boolean success;   public it6 () {       success = false;      for  (int  i = 0; i < num; i++) {          lsta.add (i);           lstb.add (i);      }    }   arraylist getb () {      while (success != true) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SETNEXTB ();      }  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;LSTB;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;VOID&NBSP;SETNEXTB () {       boolean flag =true;      for  (int i=0; i < num; i++) {         int f = getfrequency (i);          if (Lstb.get (i)  != f) {             lstb.set (i, f);             flag = false;         }      }      success =  Flag;   }   int getfrequency (Int value) {       int count = 0;      for (int i = 0; i  < num; i++) {         if (Lstb.get (i)  ==  value)             count++;       }      return count;   }    Public static void main (String[] args)  {      IT6  Test = new it6 ();       arraylist<integer> res =  test.getb ();       printgenericiterator (Test.lsta.iterator ());      &nbSp System.out.println ();       printgenericiterator (Test.lstb.iterator ());    }}

Code output:

0 1 2 3 4 5 6 7 8 9 6 2 1 0 0 0 1 0 0 0


IT company 100-based on the upper row gives 10 number, in its next row to fill out the corresponding 10 numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.