A ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ..., aN), the next n-tuple In the sequence are 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 and 8,11,2,7 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) (2, 0, 0, 0, 2)
(2, 0, 0, 2, 0) (2, 0, 2, 2, 2) (2, 2, 0, 0, 0) (0, 2, 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) ...
Given an n-tuple of integers, write a program to decide if the sequence are reaching to a zeros tuple or a per Iodic Loop.
Input
Your program was to read the input from standard input. The input consists of T test Cases. The number of test cases T is given on the first line of the input. Each test case starts with a line containing an integer n(3n), which represents the size of a Tuple in the Ducci sequences. In the following line, n integers is given which represents the n-tuple of integers. The range of integers is from 0 to 1,000. Assume that the maximum number of steps of a ducci sequence reaching zeros tuples or making a loop does not exceed A.
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 Reache s 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 Zero loop
Test instructions: Give you an array, add the number of neighbors (the last number should be the first number), and then you can get a new array, repeated such steps, the result will be two, each member of the array is zero, or in such a process occurs, the occurrence of the same as the previous occurrence of the same item.
The final request to determine what kind of situation
Ideas:
If you follow the general idea of solving a problem, each get a new array to determine whether it is zero or loop. Zero is good to say, if you judge loop that will go through the array every time, the program will definitely time out. 、
So ~ ~ ~
Anyway, there are only two possible results, not loop, or zero, just Judge zero.
Code:
#include"Iostream"#include"CString"UsingNamespace Std;Constint MAXN=1010;Constint MAXM=16;int Len;int loop=0, zero=0;int a[MAXN][maxm];IntSwift(int number){Return(number<0)?-number: number;}voidInit(){cin>>len;For(int I=0; I<len; I+ +) CIN>>a[0][i];}voidWork(){int flag=0;For(Int J=1; j<maxn; j++){For(int I=0; I<len; I++){If(I==len-1) A[j][i]=Swift(A[j-1][i]-a[j-1][0]);else a[j][i]=Swift(A[j-1][i]-a[j-1][i+1]);}For(int h=0; h<len; h++)If(A[j][h]!=0){Flag=1;Break;}If(Flag==0){Zero=1;Return;}Else flag=0;}}IntMain(){int n; Cin>>n;While(n--) {init (); Work (Zero) {cout << "ZERO" <<endl; Zero=0;} else cout<< "LOOP" <<endl;} return 0;}
Ducci Sequence Problem Solving report