C + + Training questions (the number of 2~6 times only changes the order of numbers)

Source: Internet
Author: User

Title: It can be seen the number, 125874, and its double, 251748, contains exactly the same digits, if in a different or Der Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contains the same digits.

Some numbers, such as 125874, twice times 251748, only change the order of numbers, without changing the number of numbers and a specific number. Find the smallest integer, which is twice times, 3 times times, 4 times times, 5 times times, 6 times times, and satisfies this condition.

Analysis: 1. From small to large in order to increase, first calculate 6 times times, 6 times times if the number of bits increase, then do not have to calculate the other multiples, and the number also increased one, such as 2 6 times times 12, no calculation 2, 3, 4, 5 times times, also do not have to calculate 3, 4, 5, 6, 7, 8, 9,

2. Convert numbers to strings, and use the STL standard library's own functions to handle. The sort function is a self-bringing sorting function.

Code:

Test.cpp: Defines the entry point of the console application. * Title: 125874*2=251748, two numbers have the same number, but the order of the numbers are different, to find the minimum number of x,x to meet the law twice times, 3 times times, 4 times times, 5 times times and 6 times times all meet the above conditions, the numbers are the same, the order is different. Analysis: 1. Data from small to large 2. First judge whether the cross-border when 6 times times, if the cross-border jumps directly to the next number of digits (such as the 2-digit jump to 3-digit first, such as 100 to start the operation) 3. Determine whether 5 times times, 4 times times, 3 times times, twice times are the same number, are transferred to StdAfx.h "#include <iostream> #include <algorithm> #include <string> #include <sstream># Include <cmath>using namespace std;//computes the number of digits int digit (double num) {return log10 (num) +1;} See if two numbers are the same, by transferring to the string to calculate the bool match (string str1, String str2) {sort (Str1.begin (), Str1.end ()), Sort (Str2.begin (), Str2.end ()); return str1==str2;} The process of jumping, such as jumping from 2 to 10long long init (int digit) {return long Long (POW (10.0,digit-1));} int _tmain (int argc, _tchar* argv[]) {Long long num=1;bool flag=true;stringstream ss;string data;string str;int i=0;while ( true) {int dig=digit (num);//number of input numbers//ss<<num;str=ss.str ();//continuous use remember to clear and remember 0 ss.clear (); Ss.str (""); long Long enter ;////the next digit enter=digit (num*6), if (Enter==dig) {ss<<6*num;data=ss.str (); Ss.clear (); Ss.str (""); Flag=match ( Str,daTA), if (flag==true) {for (i=5; i>1; i--) {enter=digit (i*num); Ss<<i*num;data=ss.str (); Ss.clear (); Ss.str (""); Flag=match (Str,data); if (Flag==false) {break;}} if (flag==true) break;}} else{//if * 6 digits plus 1, the number of NUM digits will have to be the first number of one digit num=init (enter); continue;} ++num;} cout<< "minimum number of compliance requirements" <<num<<endl;cout<< "Twice" <<2*num<<endl;cout<< "3 times Times" < <3*num<<endl;cout<< "4 times Times" <<4*num<<endl;cout<< "5 times Times" <<5*num<<endl;cout << "6 times Times" <<6*num<<endl;system ("pause"); return 0;}
Results:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Training questions (the number of 2~6 times only changes the order of numbers)

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.