首页 > 代码库 > 紫书第五章训练 uva 1594 Ducci Sequence by crq

紫书第五章训练 uva 1594 Ducci Sequence by crq

Description

A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1a2, ... an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers:

a1a2... 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 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 is reaching to a zeros tuple or a periodic loop.

Input

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing an integer n(3技术分享n技术分享15), which represents the size of a tuple in the Ducci sequences. In the following line, n integers are given which represents the n-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 
ZERO 
LOOP

题意分析:对一个数组不断按照以下过程进行变换(每个数变成后面一个元素与自身相减后取绝对值,最后一个数的下一个是第一个数):

(a1, a2, · · · , an) → (|a1 ? a2|, |a2 ? a3|, · · · , |an ? a1|)

到最后要么变成全部为0(输出ZERO), 要么形成死循环(LOOP)。

解决方法:因为数组元素个数最大才15,直接模拟,数组用vector记录,并用map记录每次变换后数组元素:

map<vector<int>, int> mp;

每次变换后查找map中是否已经存在,若已经存在则输出LOOP,结束。如果变换后结果是全部为0的数组,输出ZERO,结束。

AC源码:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <sstream>
 5 #include <map>
 6 using namespace std;
 7 
 8 map<vector<int>, int> mp;
 9 int myabs(int a)
10 {
11     return a>0?a:-a;
12 }
13 
14 bool Zero(vector<int> &vec)
15 {
16     for(int i=0;i<vec.size();i++)
17     {
18         if(vec[i]!=0)
19             return false;
20     }
21     return true;
22 }
23 
24 void Change(vector<int>& vec)
25 {
26     int n = vec.size();
27     int x = vec[0];
28     for(int i=0;i<n-1;i++)
29     {
30         vec[i] = myabs(vec[i+1]-vec[i]);
31     }
32     vec[n-1] = myabs(vec[n-1]-x);
33 }
34 
35 int main()
36 {
37 //    freopen("d:\\data1.in","r",stdin);
38     int cas;
39     cin>>cas;
40     while(cas--)
41     {
42         mp.clear();
43         int i, n;
44         vector<int> vec;
45         cin>>n;
46         for(i=0;i<n;i++)
47         {
48             int x;
49             cin>>x;
50             vec.push_back(x);
51         }
52         int flag = 0;
53         while(true)
54         {
55             if(mp[vec])
56             {
57                 flag = 2;//loop
58                 break;
59             }
60             mp[vec] = 1;
61             if(Zero(vec))
62             {
63                 flag = 1;//zero
64                 break;
65             }
66             Change(vec);
67         }
68         if(flag==1)
69             cout<<"ZERO"<<endl;
70         else if(flag==2)
71             cout<<"LOOP"<<endl;
72     } 
73     return 0;
74 }

 

紫书第五章训练 uva 1594 Ducci Sequence by crq