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(3n15), 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 这题的意思就是输入n个数,相邻两个数相减的绝对值赋给前一个数,最后一个数与第一个数相减。如此循环下去如果在一千次以内数列的所有数都是0了,就输出ZERO,否则输出LOOP。 代码如下:

#include <iostream>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n,a[20];
cin>>n;
int flag=0;                     //每次输入完都要把flag归零
for(int i=0;i<n;i++)
cin>>a[i];
for(int j=0;j<1000;j++)
{
int f=a[0];                     //因为先是a[0]与a[1]运算再赋值给a[0],a[0]的值会变,所以要先把a[0]的值赋给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];                     //求数组0到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];               // 再加上a[n-1]的值。
if(s==0)
{
flag=1;
break;
}
}
if(flag)
cout<<"ZERO"<<endl;
else
cout<<"LOOP"<<endl;

}
return 0;
}

第一次写博客,望各位海涵。


Ducci Sequence的更多相关文章

  1. UVA 1594 Ducci Sequence(两极问题)

           Ducci Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   D ...

  2. uva 1594 Ducci Sequence <queue,map>

    Ducci Sequence Description   A Ducci sequence is a sequence of n-tuples of integers. Given an n-tupl ...

  3. UVa----------1594(Ducci Sequence)

    题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...

  4. [刷题]算法竞赛入门经典(第2版) 5-2/UVa1594 - Ducci Sequence

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,20 ms) //UVa1594 - Ducci Sequence #include< ...

  5. UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, · · · ...

  6. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  7. Ducci Sequence UVA - 1594

      A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...

  8. Uva - 1594 - Ducci Sequence

    水题,算出每次的结果,比较是否全0,循环1000次还不是全0则LOOP AC代码: #include <iostream> #include <cstdio> #include ...

  9. UVA 1594:Ducci Sequence (模拟 Grade E)

    题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...

随机推荐

  1. uva 699 The Falling Leaves dfs实现

    额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...

  2. Spring 事务管理高级应用难点剖析--转

    第 1 部分 http://www.ibm.com/search/csass/search/?q=%E4%BA%8B%E5%8A%A1&sn=dw&lang=zh&cc=CN& ...

  3. 关于Eclipse(MyEclipse)中一次性批量导入多个项目Project.

    以前更换Eclipse(MyEclipse)的时候要想把原Eclipse中的项目导入到新的Eclipse中的做法是: 1.先把原Eclipse中工作空间中的项目(不包括.metadata文件夹)复制到 ...

  4. windows 端搭建nfs 服务器

    因为最近虚拟机桥连模式总是用不了会出问题,所以今天花了半个小时研究了一下在Windows主机下搭建一个nfs服务器进行文件传输. 其实步骤很简单,如下: 1. 下载NFS Windows服务器软件,我 ...

  5. 写css动画

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  6. Git CMD - log: Show commit logs

    命令参数 git log [<options>] [<revision range>] [[\--] <path>…​] 命令参数 --since=<date ...

  7. Android之记账本

    这个ColaBox记事本是我从网上下载下来的拿来学习一下的(APK下载点这里.) 从登记收入与开支的页面跳转到账单页面运用了SQL数据库的录入,整体表的结构为: db.execSQL("CR ...

  8. 理解CPU内存管理

    概述:从设计层面理解CPU的内存模式,包括段式内存管理.页式内存管理以及虚拟化扩展内存管理.实际上,硬件支持与软件实现从来就不是能分开讲的,比如,Intel CPU架构师在选择CPU的硬件特性时,必然 ...

  9. Linux/centos/redhat下各种压缩解压缩方式详解

    1.zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d ...

  10. 使用WDS安装Windows8.1

    WDS的部署 安装角色 配置 1. 选择配置服务器 2. 核对是否满足要求 3. 输入远程安装文件夹的路径 4. 选择是否使用自带的DHCP服务器 5. 可以保持默认 6. 完成配置后添加映像文件 7 ...