Nine-digit progressive Divisor

Source: Internet
Author: User

Calculate the nine-digit progressive divisor. The so-called nine-digit progressive divisor is such a number: This number uses the nine numbers from 1 to 9. Each number appears only once. The first two digits of the nine digits can be divided by two, and the first three digits can be divided by three ...... The first n digits can be divisible by N, and the entire nine digits can be divisible by 9.

* The problem analysis and algorithm design problems can be simplified to the following: as long as the values of each number are lifted, correct results can be obtained by judging the results according to the requirements of the question. The "progressive division" condition is given in the s problem, so that we can add condition judgment in the exhaustive method. When the value of some bits is determined, the system immediately determines whether the generated part meets the "progressive division" condition. If yes, continue to raise the next digit; otherwise, the generated digit is incorrect. In this way, we can introduce conditional judgment into the exhaustive method, discover contradictions as early as possible, and discard unnecessary exhaustive values as soon as possible to improve the program execution efficiency. In order to achieve the purpose of early identification of contradictions, the multi-cycle approach should not be used for poor quality of programming. The algorithm used in the program is not an exhaustive method, but a return method.

# Include <stdio. h> # define num 9int A [num + 1]; int main () {int I, K, flag, not_finish = 1; long sum; I = 1; // I: the array element being processed, indicating that the first I-1 element has met the requirement, processing is the I-th element a [1] = 1; // set the initial value while (not_finish) for element a [1] // not_finish = 1: The processing is not over {While (not_finish & I <= num) {for (flag = 1, k = 1; flag & K <I; k ++) if (a [k] = A [I]) Flag = 0; // determine whether the I-th element is repeated with the previous I-1 element for (sum = 0, k = 1; flag & K <= I; k ++) {sum = 10 * sum + A [k]; If (sum % K) Flag = 0; // determines whether the integer composed of the first K digits can be divisible by k} If (! Flag) // flag = 0: indicates that the I-th bit does not meet the requirements, you need to reset {if (a [I] = A [I-1]) // if the value of a [I] has been circled to catch up with a [I-1] {I --; // I value minus 1, return if (I> 1 & A [I] = num) A [I] = 1; // when the value of the I bit reaches num, take 1 else if (I = 1 & A [I] = num) for the value of the I-Bits. // when the value of the 1st-bits reaches num, end not_finish = 0; // set the end flag of the program to else a [I] ++; // take the next value for the I-bit, and Add 1} else if (a [I] = num) A [I] = 1; else a [I] ++;} else // the I-th bit has already met the requirements, process the I + 1-bit if (++ I <= num) // I + 1 to process the next element, if (A [I-1] = num) A [I] = 1; // if the value of the I-1 is num, then the value of a [I] is 1 else a [I] = A [I-1] + 1; // otherwise, the initial value of a [I] is the "Next" value of the [I-1] value} If (not_finish) {printf ("the progressive divisiable number is :"); for (k = 1; k <= num; k ++) // outputs the calculated result printf ("% d", a [k]); if (A [NUM-1] <num) A [NUM-1] ++; else a [NUM-1] = 1; not_finish = 0; printf ("\ n ");}} return 0 ;}

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.