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 ( ...
随机推荐
- POJ 2912 Rochambeau
题意:有一些人玩石头剪刀布,其中有一个人(称其为裁判)可以出“石头”,“剪刀”,“布”中的任意一个,其他人永远只能出相同的一个.即有的人只能出剪刀,有的人只能出石头,有的人只能出布.进行了多次对决,每 ...
- 网络协议- HTTP
http:是用于www浏览的一个协议.tcp:是机器之间建立连接用的到的一个协议.
- Java调用R(二)_JRI
推荐使用.相比RServe更灵活,效率更高. 基本步骤 1. R中需要安装rJava包. 2. 系统变量Path加上 C:\Program Files\R\R-3.0.1\bin\i386;C:\ ...
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)
iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...
- Unity3D 画线插件 Vectrosity_Simple2DLine
Vectrosity是一个很方便的画线插件,用它我们可以画出2D,3D,贝塞尔,圆,椭圆等各种线条图案. :链接: http://pan.baidu.com/s/1pJjTFjt 密码: uesn 首 ...
- axf、elf文件转换成bin、hex脚本工具
在嵌入式开发过程中常常遇到将axf或elf文件转换成bin的情况,大家都知道通过gnu toolchain中的objcopy和keil中的fromelf能做到.可是为了这么一个小事而记住复杂的选项以及 ...
- mysql批量删除指定前缀或后缀表
今天突然发现我们数据库中多出很多表,后缀名为"copy",预计是navicat直接拷贝导致的,然后要对这些有同样后缀名的表进行删除,假设一个一个选择会非常麻烦,表计较多,在网上找了 ...
- GIT使用指南
安装git,svn,ant,maven并配置环境变量 1.拷贝settings.xml到用户目录的.m2目录下. 2.打开git命令行,使用如下命令生成公钥私钥 ssh-keygen -t rsa 3 ...
- java mysql驱动
mysql驱动方式有三种, 1.第一种是先把jar包放在项目的目录下,通过添加jar包,是使用相对地址的,这样把项目复制到其它电脑也可以用 2.第二种方法是导入外部的jar包,是绝对地址,如果项目要复 ...
- stl_alloc.h
/* * Copyright (c) 1996-1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, ...