"Programming Marathon" "007-loop Number"

Source: Internet
Author: User

"Programming Marathon algorithm directory >>>" "007-Cycle Count" "Engineering download >>>" 1 Topic Description

142857 is a six-digit number that we found:
142857*1=142857
142857*2=285714
142857*3=428571
142857*4=571428
142857*5=714285
142857*6=857142
That is, 1 to 6 of the integer to multiply 142857, you will get a number of the original end of the loop to move a number of numbers and then broken in some places to get the number.
In other words, if the original number and the new number are both end to end, they get the same ring. It is not necessarily the same starting number for two numbers.
Please write a program that determines that the given number is not the number of loops.

1.1 Input Description:

The input includes multiple sets of data.
Each set of data contains a positive integer n,n is a positive integer of 2 to 60 bits, and the prefix 0 is allowed. That is, 001 is also legal input data.

1.2 Output Description:

For each set of data, the output is "yes" if it is the number of loops, otherwise the output is "No".

1.3 Input Example:
142857012345
1.4 Output Example:
YesNo
2 ideas for solving problems

Assuming that the number you are seeking is N, which is M-bit, then n can be represented as x_ (m-1) x_ (m-2) ... x_0, because N has more bits, you can use an integer array num to represent the value of the input int array with the size m,num[i]= x_i. Each subscript in num represents a digit.
Whether n is the number of loops, as long as the number of num multiplied by 2, 3, 4, 5, 6 is a loop count. Assuming that num multiplied by K (k=2, 3, 4, 5, 6) is the result of RET, if RET gets more data bits than num, then num is not the number of loops, if the number of data bits, then the RET loop move, find the mobile digital ret^ ' use ret^ ' is equal to num, if no description num is not a number of loops. When k is equal after all the numbers are taken, NUM is the number of loops. The equivalent n is the number of loops, otherwise n is not the number of loops.

3 Algorithm Implementation
ImportJava.util.Scanner;/** * Author: Wang Junshu * time:2016-05-09 06:56 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public  class Main {     Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt"));         while(Scanner.hasnext ())            {String n = scanner.next ();        System.out.println (Cyclenumber (n));    } scanner.close (); }/** * Determines whether n is the number of loops * * @param N Numeric String * @return No: Not the number of loops, yes: is the number of loops * *    Private StaticStringCyclenumber(String N) {int[] num =New int[N.length ()];//Numeric string conversions number represented by an array        //subscript from small to large indicates low to high,         for(inti =0; I < n.length (); i++) {Num[i] = N.charat (N.length ()-1-i)-' 0 '; }//system.out.println (n + arrays.tostring (num));         for(inti =2; I <=6; i++) {if(!check (num, i)) {return "No"; }        }return "Yes"; }/** * Checks if the number represented by an array is multiplied by N, is the number of loops * * @param The number represented by the NUM array, denoted by small to large, low to high * @param n Digits * @return true: is the number of loops, false: not the number of loops */    Private Static Boolean Check(int[] num,intN) {//From the low carry        intcarry =0;//result array        int[] ret =New int[Num.length];intT for(inti =0; i < num.length;            i++) {T = carry + num[i] * n; Ret[i] = t%Ten; Carry = t/Ten; }//After the calculation there is a note after multiplying the results of one more, must not loop number        if(Carry! =0) {return false; }//Loop the number in the RET to construct a new RET to see if it is equal to Num         for(inti =0; i < ret.length; i++) {//Find the location where you want the loop to move            if(num[0] = = Ret[i]) {Exchange (ret, i);if(Equal (ret, num)) {return true; }//If not equal, restoreExchange (ret, ret.length-i); }        }return false; }/** * Moves the array arr loop to move num bits * num will array arr into [0, num-1], [num, arr.length-1] Two parts, first will [0, num-1] Flip * and then [num, Arr.length-1] Flips, and finally flips [0, arr.length-1] to get results * * @param arr array * @param number of digits moved by Num */    Private Static void Exchange(int[] arr,intNUM) {if(arr = =NULL|| Num <1|| num > Arr.length) {return; } Exchange (arr,0, Num-1); Exchange (arr, num, arr.length-1); Exchange (arr,0, Arr.length-1); }/** * Flips all the elements of the series arr from the To position * * @param arr array * @param from start position * @pa Ram to end position * /    Private Static void Exchange(int[] arr,intFromintTo) {if(arr = =NULL|| From <0|| From > Arr.length | | to <0|| To > Arr.length) {return; }intT while(From < to)            {t = Arr[from];            Arr[from] = arr[to];            Arr[to] = t;            from++;        to--; }    }/** * Compare the contents of two arrays for equality * * @param a array * @param b array * @return true: Equal, FAL SE: Unequal */    Private Static Boolean Equal(int[] A,int[] b) {if(A = =NULL&& b = =NULL) {return true; }if(A = =NULL|| b = =NULL|| A.length! = b.length) {return false; } for(inti =0; i < a.length; i++) {if(A[i]! = B[i]) {return false; }        }return true; }}
4 Test Results

5 Other information

because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.

"Programming Marathon" "007-loop Number"

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.