c++20701 Division (Rujia 1, 2 volume seventh, Mob search solution)

Source: Internet
Author: User
Tags repetition

20701 Division
Difficulty level: B; programming language: Unlimited; run time limit: 1000ms; run space limit: 51200KB; code length limit: 2000000B
Question Description
Enter positive integer n to output all ABCDE and fghij that satisfy the expression abcde/fghij=n in order from small to large, where a~j happens to be an arrangement of the number 0~9. if the number of test instructions is not met, the output is 0. The five-digit number mentioned in the subject can include a leading 0, such as 01234, which is also called a five-digit number.
Input
A positive integer n
Output
Several rows, each of which consists of two qualifying five-bit positive integers (two of each row are first large and small), and two numbers are separated by a space.
Input example
62
Output example
79546 01283
94736 01528
Other Notes
Data range 2≤n≤79; from small to Osashi in the title, the first number of each row of output data is the order of small to large.

According to test instructions, this problem can be solved with a mob search, but the general search in this topic to each one with a cycle, the problem 2 number, each number 5 bits (million bits can be 0), supposedly 10 digits, 10 cycles, right? But here you just enumerate one number and the other number can be calculated. We certainly choose to enumerate the small number (in fact, which is the same), that is, the divisor, then dividend should use this enumeration of the number of Xn, if the enumeration of the large number, that is dividend, then the divisor should use this enumeration of the number of ÷n. We use the P[10] array to store these two numbers and to make it easy to find duplicates all at once. This is the most basic approach and the best way to understand it.

This question is Rujia a volume of P114 or Rujia two copies of the title of P182, the seventh "Violence Solution" the first question, just talk about the idea, did not give the code. Before you say this question, why do you have to enumerate a number only. We can know that if all permutations of enumeration 0-9, there will be 10! = 3628800 cycles, which sounds a little too much for enumeration, right? If you enumerate only one, the loop amount will fall to 5! = 120 rounds, the calculation of the number of the program!

Here is the code, with the most stupid mob search, this question N maximum 79, and each kind of data is not much possibility, so the mob can do.

This program enumerates the divisor and then takes out the dividend.

#include <iostream>
using namespace std;
int n,a,b,p[10],h,i;
bool Isit,ih;//isit Check whether each group of dividend and divisor is legal, IH check whether there is a final result
int main ()
{
cin>>n;
For (p[0]=0;p[0]<=9;p[0]++)//Except tens of thousands of bits
For (p[1]=0;p[1]<=9;p[1]++)//except thousands of bits
For (p[2]=0;p[2]<=9;p[2]++)//except hundreds of bits
For (p[3]=0;p[3]<=9;p[3]++)//except dozens of bits
For (p[4]=0;p[4]<=9;p[4]++)//Except for a few bits
                    {
if (p[0]!=p[1] && p[0]!=p[2] && p[0]!=p[3] && p[0]!=p[4] && p[1 ]!=P[2] && p[1]!=p[3] && p[1]!=p[4] && p[2]!=p[3] && p[2]!=p[4] && p[3]!=p[4])// Find if the divisor has duplicate numbers. In fact, this can save a lot of back dividend calculation! If the divisor has a repetition of dividend, a look at a lot of repetition, will do a lot of useless.
                        {
a=p[0]*10000+p[1]*1000+p[2]*100+p[3]*10+p[4];//Calculate divisor
b=a*n;//figure out dividend
if (9999<b && b<100000) Make sure 10000<= dividend <=99999, think about why the divisor can not be less than 1w?

p[5]=b/10000;//The following is the separation dividend
p[6]= (b%10000)/1000;
p[7]= (b%1000)/100;
p[8]= (b%100)/10;
p[9]=b%10;
isit=1;//See if this group number is no duplicate number
For (h=0;h<=9;h++)
For (i=h+1;i<=9;i++)
if (P[h]==p[i]) isit=0;//is the divisor and divisor full row to find there are no duplicates
if (isit)//If it is 1, no repetition
                                        {
ih=1;//Make sure there's a result
For (i=5;i<=9;i++) cout<<p[i];//Remember to hit dividend first! Ignoring the one thing that can be directly output B
cout<< ";
For (i=0;i<=4;i++) cout<<p[i];//after the divisor!
cout<<endl;
                                        }
                                }
                        }
                    }
if (!ih) cout<< ' 0 ';//no result 0
return 0;
}

SCX released in 2015.7.9

My Computer slag, self-written theory--

c++20701 Division (Rujia 1, 2 volume seventh, Mob search solution)

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.