UVA 1594 Ducci Sequence(两极问题)
Ducci Sequence
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
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(3n
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
题解:给定数组,依次求前一个减后一个的值的绝对值,如果是最后一个则是最后一个减第一个的值的绝对值,
最多循环1000次,如果出现数组的值全部变为0,则为ZERO,否则为LOOP,所以只求全部为0的情况即可。
#include<iostream>
using namespace std;
int main()
{int a[];
int i,j,t,n,sum;
cin>>t;
while(t--)
{
cin>>n;
for(i=; i<n; i++)
cin>>a[i];
for(i=; i<; i++)
{ sum=;
int s=a[];
for(j=; j<n-; j++)
{
if(a[j]>a[j+])
a[j]=a[j]-a[j+];
else
a[j]=a[j+]-a[j];
sum+=a[j]; } if(a[n-]>s)
a[n-]=a[n-]-s;
else a[n-]=s-a[n-];
sum+=a[n-];
if(sum==)
break; } if(sum==)
cout<<"ZERO"<<endl;
else
cout<<"LOOP"<<endl; }
return ;
}
UVA 1594 Ducci Sequence(两极问题)的更多相关文章
- uva 1594 Ducci Sequence <queue,map>
Ducci Sequence Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tupl ...
- 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, · · · ...
- Uva - 1594 - Ducci Sequence
水题,算出每次的结果,比较是否全0,循环1000次还不是全0则LOOP AC代码: #include <iostream> #include <cstdio> #include ...
- 【暴力模拟】UVA 1594 - Ducci Sequence
想麻烦了.这题真的那么水啊..直接暴力模拟,1000次(看了网上的200次就能A)后判断是否全为0,否则就是LOOP: #include <iostream> #include <s ...
- 【UVA】1594 Ducci Sequence(纯模拟)
题目 题目 分析 真的快疯了,中午交了一题WA了好久,最后发现最后一个数据不能加\n,于是这次学乖了,最后一组不输出\n,于是WA了好几发,最后从Udebug发现最后一组是要输出的!!! ...
- UVa----------1594(Ducci Sequence)
题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...
- Ducci Sequence UVA - 1594
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...
- UVa 1584 Circular Sequence --- 水题
UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...
- Ducci Sequence
Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...
随机推荐
- Java nextInt()函数
nextInt( int num) 能接受一个整数作为它所产生的随机整数的上限,下限为零,比如:nextInt(4)将产生0,1,2,3这4个数字中的任何一个数字,注意这里不是0-4,而是0-3..但 ...
- SPBF(队列优化的Bellman-Foord)
- 简单的Goto运算演示程序
/* * 该程序用于计算某个项集的Goto集 * RexfieldVon * 2013年8月11日2:34:50 */ #include <stdio.h> #include <st ...
- kafka Disks and Filesystem(磁盘和文件系统)
转载请注明来源地址:http://www.cnblogs.com/dongxiao-yang/p/5206631.html We recommend using multiple drives to ...
- TCP具体解释(3):重传、流量控制、拥塞控制……
传输数据 在TCP的数据传送状态.非常多重要的机制保证了TCP的可靠性和强壮性.它们包括:使用序号.对收到的TCP报文段进行排序以及检測反复的数据:使用校验和来检測报文段的错误.使用确认和计时器来检測 ...
- Android Matrix详解
Matrix的数学原理 平移变换 旋转变换 缩放变换 错切变换 对称变换 代码验证 Matrix的数学原理 在Android中,如果你用Matrix进行过图像处理,那么一定知道Matrix这个类.An ...
- [转] Java快速教程
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是面向对象语言.这门语言其实相当年轻,于1995年才出现,由Sun公司出品 ...
- ubuntu 查看端口被占用并处理
当启动程序出现端口号被占用的情况,需要查看端口使用情况,使用netstat命令,下面是常用的几个查看端口情况的命令:查看所有的服务端口(ESTABLISHED netstat -a查看所有的服务端口, ...
- Day12 - 堡垒机开发
Python之路,Day12 - 那就做个堡垒机吧 本节内容 项目实战:运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多 ...
- eclipse开发servlet应用,Tomcat无法访问jpg图片 ===第二版===
之前版本中,设置完后,确实可以访问图片了,但是问题接着来了,那就是,无法访问servlet的服务了. 后来想了下,原因也挺好理解的,设置到了Tomcat目录,而项目没有部署,所以没能访问. 但是怎么两 ...