Algorithm for calculating All integers in the n-bit m-Base

Source: Internet
Author: User

 

Algorithm source: the art of computer programming vol 4

Algorithm ideas:

 

1. Record n as an integer, a1a2... An, initialized to 0... 00.

 

2. Make j = n and calculate aj = aj + 1. When aj is equal to S-1, enter 1 to the high bit, and set this bit to 0. Repeat this step for all. Until all items are traversed.

 

 

 

C language implementation of this algorithm:

 

 

 

 

Void mixed_radix_num (int n, int radix)

{

Int j;

Int * a = (int *) malloc (sizeof (int) * (n + 1 ));

If (! A) return;

For (j = 0; j <= n; ++ j)

* (A + j) = 0;

While (1 ){

Print_num (a, n );

J = n;

While (* (a + j) = (radix-1 ))

{

* (A + j) = 0;

J --;

}

If (j = 0) break;

* (A + j) = * (a + j) + 1;

}

Free ();

A = 0;

}

 

 

 

Key to this Code: for n-bit integers, apply for n + 1 integer space, a [0] As the sentry bit.

Related Article

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.