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 < ...
随机推荐
- vscode 配置表
{ "git.ignoreMissingGitWarning": true, "editor.multiCursorModifier": "ctrlC ...
- 2021-2-25:对于 Java MMAP,如何查看文件映射脏页,如何统计MMAP的内存大小?
我们写一个测试程序: public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFi ...
- Vue学习笔记-Windows系统Git安装(按装vue-element-admin报错)
一 使用环境: windows 7 64位操作系统 二 Windows系统Git安装(Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理,是目前使用范围最广的版 ...
- Django框架-cookie和session以及中间件
目录 一.cookie 和 session 1.为什么会有这些技术 2. cookie 2.1 Django如何设置cookie 2.2 Django如何获取cookie 2.3 Django如何设置 ...
- “蚂蚁牙黑”太火,想玩就用ModelArts做一个!
摘要:本文将介绍如何借力一站式 AI 开发平台,"傻瓜式"操作实现生成"蚂蚁牙黑"小视频. 作者:华为云EI专家胡琦 一夜之间,朋友圈都在"蚂蚁牙黑& ...
- eureka server 配置
启动类 import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure. ...
- Redis之面试连环炮
目录 1.简单介绍一下Redis 2.分布式缓存常见的技术选型方案有哪些? 3.Redis和Memcached的区别和共同点 4. 缓存数据的处理流程是怎样的? 5. 为什么要用 Redis/为什么要 ...
- [转载]Android MVC,MVP和MVVM 思想&例子
在Android开发中,常采用 MVC(Model-View-Controller)或者MVP(Model-View-Presenter) 等框架模式.设计如图 mvc mvp 可以看出,在 MV ...
- 如何在 C# 中使用 const,readonly,static
平时在开发时经常会用到 const,readonly,static 关键字,可以肯定这些关键词是完全不同的概念,但有时候他们在用法上很相似以至于在场景中不知道选择哪一个,这篇文章我们就来讨论 C# 中 ...
- 从代理模式 到 SpringAOP
前言 Spring AOP 就是通过代理模式来实现切面编程的.代理模式用来为其他对象提供一种代理,以控制对这个对象的访问. 代理对象在客户端和目标对象之间起到中介的作用.通过控制对这个对象的访问,可以 ...