[algorithm] fixed elements are fully arranged on fixed lengths

Source: Internet
Author: User

Topic 1: Print all numbers from 1 to the maximum number of n digits. For example, N is 3, then print 1,2,3,4...999.

Title 2: Throw n dice on the ground, and the sum of the points on the top side of the dice is S. Enter N to print out the probability that all possible values of s will appear.

----------------

For topic 1, because the value of n can be large, direct print is not possible, and no data type can carry large n. For topic 2, the probability is to find the number of points and the frequency of the throw of S. These two topics have one thing in common, and they all have to do with a sequence, which can be described as: there are n boxes, each of which can be placed X1 to any number of Xn, outputting all possible emission patterns (possibly ordered). For topic 1, there are n boxes, each containing 0-9 of any number in the box, and a print sequence. For the title 2, it is N boxes, each box contains 1-6 of any number, counting the number of occurrences of the element in the box.

---------------

Pseudo-code for topic 1:

int* s=New int[n+1];//use S[n] to record the nth bit, S is the boxvoidPRINTN (intN//print 1 to maximum n-bit integers{   for(intI=0;i<Ten; i++) {S[n]=i; if(n>1) {PRINTN (n-1); }      Else if(n==1) {print (S); //might need some processing, upside down, to knock off unwanted 0.    } }    }    

For Topic 2, we don't need an array to represent the box, because we don't relate to what's in the box, we only care about the elements inside.

voidGetappeartimes (intNintEndintCursum,int*probility) {     for(intI=1; i<=6; i++)    {        if(end==0)        {            //Probility[i] Storage and throw occurrences for Iprobility[cursum+i]++; }        Else{getappeartimes (n,end-1, cursum+i,probility); }    }}

[algorithm] fixed elements are fully arranged on fixed lengths

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.