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 ( ...
随机推荐
- [Locked] Generalized Abbreviation
Write a function to generate the generalized abbreviations of a word. Example:Given word = "wor ...
- multipath.conf
# This is a basic configuration file with some examples, for device mapper# multipath.# For a comple ...
- Unity3D NGUI制作进度条
利用GUI可以制作进度条,但是NGUI更加方便 我是用的NGUI3.5.3, 先找到NGUI Slider的预制体,利用自带的UISlider来制作. 主要是利用UISlider的Value来控制进 ...
- PKU 1511 Invitation Cards (SPFA+邻接表)
题目链接:点击打开链接 题目需要求从原点到所有点的最短距离之和和所有点到原点的最短距离之和,在求所有点到原点最短距离的时候用到了一个技巧:即把图反向,求原点到所有其他点的最短距离,这样用一次SPFA就 ...
- HeadFirst设计模式笔记:(六)命令模式 —— 封装调用
1.概念 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化与不变的因素. 在面向对象的程序 ...
- html contenteditable
contenteditable 是html中的一個屬性,在HTML中,某些元素設置 contenteditable='true' 屬性時可以開啟該元素的編輯模式,contenteditable 可以 ...
- log4j配置文件详解---转
使用步骤注意: 1. 从http://logging.apache.org/log4j/1.2/ 下载文件 2. 在src目录下加入log4j.properties,将jar包放入build -pat ...
- Getting Started with the NDK
The Native Development Kit (NDK) is a set of tools that allow you to leverage C and C++ code in your ...
- 当ViewPager嵌套在ScrollView/ListView里时,手势冲突如何处理?
有时我们需要将ViewPager嵌套在其他已经含有手势动作的ViewGroup里,如ScrollView,ListView时,会造成手势冲突,如表现为ViewPager向左划时,不小心向上移动了一点距 ...
- EXCEL表格常用函数使用的难点
1. INDIRECT(ref_text,逻辑值) 返回由文本字符串指定的引用.此函数立即对引用进行计算,并显示其内容.ref_text,文本引用说明, (1) A1-样式的引用(逻辑值,T,缺省) ...