UVa----------1594(Ducci Sequence)

题目:

1594 - Ducci Sequence

Asia - Seoul - 2009/2010
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 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 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
4723 - Ducci Sequence 1/25
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
Seoul 2009-2010
4723 - Ducci Sequence 2/2

分析:题目很长,简单的来说是对于一个n元数组(a[0],a[1],a[2],…… ,a[n-1]),可以根据每个数与其下一个数的差的绝对值求出一个新的n元数组(注意最后一个数应该是原数组最后一个数与原数组第一个数的差的绝对值),即(|a[0]-a[1]|,|a[1]-a[2]|,…… ,|a[n-1]-a[0]|),这个数列就是ducci数列了,而你的任务就是判断ducci数列最终会不会变成0数列,或者会循环(输入保证最多1000步就会变成0或者循环)。若最终归零,应该输出ZERO,否则输出LOOP。例如(8,11, 2, 7)->(3, 9, 5, 1)-> (6, 4,4,2) ->(2, 2,2,2) -> (0,0, 0, 0),应输出ZERO。

  因为题目输入的要求是:输入保证最多1000步就会变成0或者循环,那么我们只要在指定次数中判断出其中一种情况就可以了,其中很明显的是判断数组全变成0要比判断循环的情况要容易的多(不然每一次都需要将得到的新的数组与最先的数组一一比较,浪费时间也不讨好)。为了判断数组最终是否为0,我选择每次计算出新数组所有元素的和sum。若每次判断(最多判断1000次)过程中出现sum = 0的情况,应终止循环,输出答案ZERO,否则程序在结束1000次的判断后输出LOOP。

代码如下:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int maxn = 20;
 5 int a[maxn];
 6 int main(){
 7     int T;
 8     int n;
 9     scanf("%d", &T);
10     while(T--){
11         scanf("%d", &n);
12         for(int i = 0; i < n; i++)
13             scanf("%d", &a[i]);
14         bool isZero = false;
15         for(int j = 0; j < 1000; j++){
16             int sum = 0;
17             int first = a[0];
18             for(int i = 0; i < n - 1; i++){
19                 a[i] = abs(a[i] - a[i + 1]);
20                 sum += a[i];
21             }
22             a[n - 1] = abs(a[n - 1] - first);
23             sum += a[n - 1];
24             if(!sum) {isZero = true; break;}
25         }
26         if(isZero)    printf("ZERO\n");
27         else    printf("LOOP\n");
28     }
29     return 0;
30 }

                                                                      2015-07-04文 

  

posted on 2015-07-04 14:06  脚踏实地-仰望星空  阅读(249)  评论(0编辑  收藏  举报

导航