Ducci Sequence UVA - 1594
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.
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
HINT
采用思路时使用两个数才存储,其中一个用于计算,另一个用于判断结束,也就是排序找出最大值,如果过最大值为0那就不是循环。另外还要计算判断次数。耗时370ms。
Accepted
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int m, n,t,i;
cin >> m;
while (m--) {
cin >> i;
n = i;
vector<int>arr,arr2;
while (i--)
{
cin >> t;
arr.push_back(t);
arr2.push_back(t);
}
for (i = 1;;i++)
{
t = arr[0];
for (int j = 0;j < n - 1;j++)
arr[j]= arr2[j] = abs(arr[j] - arr[j + 1]);
arr[n - 1] = arr2[n - 1] = abs(arr[n - 1] - t);
if (i > 1000) {
cout << "LOOP" << endl;
break;
}
sort(arr2.begin(), arr2.end());
if (!arr2[arr2.size() - 1])break;
}
if (i <= 1000)cout << "ZERO" << endl;
}
}
Ducci Sequence UVA - 1594的更多相关文章
- UVA 1594 Ducci Sequence(两极问题)
Ducci Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- 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)
题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...
- Ducci Sequence
Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...
- [刷题]算法竞赛入门经典(第2版) 5-2/UVa1594 - Ducci Sequence
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,20 ms) //UVa1594 - Ducci Sequence #include< ...
- Ducci Sequence解题报告
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 (模拟 Grade E)
题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...
随机推荐
- 不可不知的 JVM 预热
一.JVM 架构基础 JVM 进程启动时,ClassLoader 会将需要的所有类加载到内存,主要分为以下三步: Bootstrap Class: 核心类库,由 "Bootstrap Cla ...
- webpack理解
打包工具 可以将多个静态文件打包成一个静态文件例如将1.js.2.js.3.css打包成h.js一个静态文件 这样做的好处是:可以减少页面的请求次数 以往是请求多个静态页面,使用webpack后会减少 ...
- AdoptOpenJDK是什么?
要搞清楚AdoptOpenJDK是什么,前提条件是我们需要知道JDK是什么,OpenJDK是什么.明白了JDK和OpenJDK的关系,会容易明白什么是AdoptOpenJDK. JDK是什么? 首先, ...
- Element-UI远程搜索功能详解
官方代码: <template> <div> <el-autocomplete v-model="state" :fetch-suggestions= ...
- 如何用Eggjs从零开始开发一个项目(1)
前言 "纸上得来终觉浅,绝知此事要躬行."虽然node一直在断断续续地学,但总是东一榔头西一榔头的,没有一点系统,所以打算写一个项目来串联一下之前的学习成果. 为什么选择Eggjs ...
- Linux 虚拟文件系统四大对象:超级块、inode、dentry、file之间关系
更多嵌入式原创文章,请关注公众号:一口Linux 一:文件系统 1. 什么是文件系统? 操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统. 通常文件系统是用于存储和组织文件的一 ...
- XAPKInstaller - XAPK游戏包安装器
XAPKInstaller 一个用于安装XAPK游戏包的安装器. 程序需要读写存储与获取已安装应用权限才可正常运行. 长按条目可显示文件的详细信息. SDK小于24(Android N)的设备会显示应 ...
- LeetCode-重建二叉树(前序遍历+中序遍历)
重建二叉树 LeetCode-105 首次需要知道前序遍历和中序遍历的性质. 解题思路如下:首先使用前序比遍历找到根节点,然后使用中序遍历找到左右子树的范围,再分别对左右子树实施递归重建. 本题的难点 ...
- SQL注入绕过waf的一万种姿势
绕过waf分类: 白盒绕过: 针对代码审计,有的waf采用代码的方式,编写过滤函数,如下blacklist()函数所示: 1 ........ 2 3 $id=$_GET['id']; 4 5 $ ...
- shell脚本,mysql数据库的备份-2[mysqldump]
# 数据库IPIP=127.0.0.1# 数据库端口PORT=3306# 数据库用户USER=root# 数据库密码PASSWORD=****# 要备份的数据库TARGET_DB=database_n ...