(Find out the form of ABCDE/fghij = N-Brute Force enumeration)

Source: Internet
Author: User


Division Time limit:3000 Ms Memory limit:0 KB 64bit Io format:% LLD & % LlU

Description


Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits0Through9Once each, such that the first number divided by the second is equal to an integerN, Where. That is,


ABCDE/fghij = N

Where each letter represents a different digit. the first digit of one of the numerals is allowed to be zero.

Input each line of the input file consists of a valid integer N. An input of zero is to terminate the program.

Output your program have to display all qualifying pairs of numerals, sorted by increasing numerator (and, of course, denominator ).

Your output shocould be in the following general form:


XXXXX/XXXXX = N

XXXXX/XXXXX = N

.

.


In case there are no pairs of numerals satisfying the condition, you must write''There are no solutionsN.". Separate the output for two different valuesNBy a blank line.

Sample Input
61620

Sample output
There are no solutions for 61.79546 / 01283 = 6294736 / 01528 = 62


Miguel Revilla
2000-08-31

Question:

As shown in the sample.

Solution:

Brute force enumeration. It is faster to start searching for molecules from the denominator, And the sprintf function is also used to make it easier to determine whether or not the numerator is heavy.

Code:

# Include <iostream> # include <cstdio> # include <string> # include <cstring> using namespace STD; int N, num, den; char buffer [20]; bool visited [10]; bool havecommonelement (int A, int B) {int temp; sprintf (buffer, "% 05d % 05d", a, B ); // The buffer here must be char and cannot be string. memset (visited, false, sizeof (visited); For (INT I = 0; I <strlen (buffer); I ++) {temp = buffer [I]-'0'; If (visited [temp]) return true; else visited [temp] = true;} return F Alse;} int main () {int Casen = 0, flag; while (scanf ("% d", & N )! = EOF & N) {If (CASEN> 0) printf ("\ n"); Casen ++; flag = 0; For (INT den = 1234; den <100000; den ++) {// the molecule is controlled within 5 widths, so it must be less than 100000. if (Den % N = 0) {// previously, it takes too much time to obtain the denominator using known molecules. After using the known denominator to obtain the denominator, because only the entire division enters the subfunction, therefore, the efficiency has improved a lot. num = den/N; If (! Havecommonelement (Num, Den) {printf ("% 05d/% 05d = % d \ n", Den, num, n); flag = 1 ;}}} if (flag = 0) printf ("there are no solutions for % d. \ n ", 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.