Ducci Sequence, duccisequence

Source: Internet
Author: User

Ducci Sequence, duccisequence

Description

 

A Ducci sequence is a sequenceN-Tuples of integers. GivenN-Tuple of integers (A1,A2 ,...,AN), the nextN-Tuple in the sequence is formed by taking the absolute differences of neighboring integers:

 

 

( A1, A2 ,..., AN) (| A1- A2 |, | A2- A3 |,..., | AN- A1 |)

 

Ducci sequences either reach a tuple of zeros or fall into a periodic loop. For example, the 4-tuple sequence starting with, takes 5 steps to reach the zeros tuple:

 

 

(8, 11, 2, 7) (3, 9, 5, 1) (6, 4, 4, 2) (2, 0, 2, 4) (2, 2, 2, 2) (0, 0, 0, 0 ).

 

The 5-tuple sequence starting with 4,2, 0,2, 0 enters a loop after 2 steps:

 

 

(4, 2, 0, 2, 0) (2, 2, 2, 2, 4 )( 0, 0, 0, 2, 2) (0, 0, 2, 0, 2) (0, 2, 2, 2) (2, 0, 0, 0, 2)

 

 

(2, 0, 0, 2, 0) (2, 0, 2, 2) (2, 2, 0, 0, 0) (0, 2, 0, 0, 0, 2) (2, 2, 0, 2, 2) (0, 2, 2, 0, 0)

 

 

(2, 0, 2, 0, 0) (2, 2, 2, 0, 2) (0, 0, 2, 2, 0) (0, 2, 0, 2, 0) (2, 2, 2, 2, 0 )( 0, 0, 0, 2, 2)...

 

GivenN-Tuple of integers, write a program to decide if the sequence is reaching to a zeros tuple or a periodic loop.

 

Input

Your program is to read the input from standard input. The input consistsTTest cases. The number of test casesTIs given in the first line of the input. Each test case starts with a line containing an integerN(3N15), which represents the size of a tuple in the Ducci sequences. In the following line,NIntegers are given which representsN-Tuple of integers. The range of integers are from 0 to 1,000. You may assume that the maximum number of steps of a Ducci sequence reaching zeros tuple or making a loop does not exceed 1,000.

 

Output

Your program is to write to standard output. Print exactly one line for each test case. Print'LOOP'If the Ducci sequence falls into a periodic loop, print'ZERO'If the Ducci sequence reaches to a zeros tuple.

The following shows sample input and output for four test cases.

 

Sample Input

 

4 4 8 11 2 7 5 4 2 0 2 0 7 0 0 0 0 0 0 0 6 1 2 3 1 2 3

 

Sample Output

 

ZERO LOOP



This means that the number of n is input, and the absolute values of the two adjacent numbers are assigned to the previous number, and the last number is subtracted from the first number. If all the numbers of the series within one thousand times are 0, ZERO is output; otherwise, LOOP is output.


The Code is as follows:

# Include <iostream>
Using namespace std;
Int main ()
{
Int T;
Cin> T;
While (T --)
{
Int n, a [20];
Cin> n;
Int flag = 0; // set the flag to zero after each input.
For (int I = 0; I <n; I ++)
Cin> a [I];
For (int j = 0; j <1000; j ++)
{
Int f = a [0]; // because the first operation is a [0] And a [1] and then assigned to a [0], the value of a [0] will change, therefore, you must first assign the value of a [0] to f.
Int s = 0;
For (int I = 0; I <n-1; I ++)
{
If (a [I]> = a [I + 1])
A [I] = a [I]-a [I + 1];
Else
A [I] = a [I + 1]-a [I];
S + = a [I]; // evaluate the sum of array 0 to N-2.
}
If (f> = a [n-1])
A [n-1] = f-a [n-1];
Else
A [n-1] = a [n-1]-f;
S + = a [n-1]; // Add the [n-1] value.
If (s = 0)
{
Flag = 1;
Break;
}
}
If (flag)
Cout <"ZERO" <endl;
Else
Cout <"LOOP" <endl;

}
Return 0;
}

 

 

The first time I wrote a blog, I hope you can understand.



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.