UVa----------1594(Ducci Sequence)
题目:
1594 - Ducci Sequence
Asia - Seoul - 2009/2010
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.
The following shows sample input and output for four test cases.
Sample Input
4
4
8 11 2 7
4723 - Ducci Sequence 1/25
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
Seoul 2009-2010
4723 - Ducci Sequence 2/2
分析:题目很长,简单的来说是对于一个n元数组(a[0],a[1],a[2],…… ,a[n-1]),可以根据每个数与其下一个数的差的绝对值求出一个新的n元数组(注意最后一个数应该是原数组最后一个数与原数组第一个数的差的绝对值),即(|a[0]-a[1]|,|a[1]-a[2]|,…… ,|a[n-1]-a[0]|),这个数列就是ducci数列了,而你的任务就是判断ducci数列最终会不会变成0数列,或者会循环(输入保证最多1000步就会变成0或者循环)。若最终归零,应该输出ZERO,否则输出LOOP。例如(8,11, 2, 7)->(3, 9, 5, 1)-> (6, 4,4,2) ->(2, 2,2,2) -> (0,0, 0, 0),应输出ZERO。
因为题目输入的要求是:输入保证最多1000步就会变成0或者循环,那么我们只要在指定次数中判断出其中一种情况就可以了,其中很明显的是判断数组全变成0要比判断循环的情况要容易的多(不然每一次都需要将得到的新的数组与最先的数组一一比较,浪费时间也不讨好)。为了判断数组最终是否为0,我选择每次计算出新数组所有元素的和sum。若每次判断(最多判断1000次)过程中出现sum = 0的情况,应终止循环,输出答案ZERO,否则程序在结束1000次的判断后输出LOOP。
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int a[maxn];
int main(){
int T;
int n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d", &a[i]);
bool isZero = false;
for(int j = ; j < ; j++){
int sum = ;
int first = a[];
for(int i = ; i < n - ; i++){
a[i] = abs(a[i] - a[i + ]);
sum += a[i];
}
a[n - ] = abs(a[n - ] - first);
sum += a[n - ];
if(!sum) {isZero = true; break;}
}
if(isZero) printf("ZERO\n");
else printf("LOOP\n");
}
return ;
}
2015-07-04文
UVa----------1594(Ducci Sequence)的更多相关文章
- UVA 1594:Ducci Sequence (模拟 Grade E)
题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...
- 【每天一题ACM】 斐波那契数列(Fibonacci sequence)的实现
最近因为一些原因需要接触一些ACM的东西,想想写个blog当作笔记吧!同时也给有需要的人一些参考 话不多说,关于斐波那契数列(Fibonacci sequence)不了解的同学可以看看百度百科之类的, ...
- HTML字符实体(Character Entities),转义字符串(Escape Sequence)
为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...
- HTML字符实体(Character Entities),转义字符串(Escape Sequence)【转】
为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...
- HTML字符实体(Character Entities),转义字符串(Escape Sequence) 转
为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...
- SQL Server ->> 斐波那契数列(Fibonacci sequence)
斐波那契数列(Fibonacci sequence)的T-SQL实现 ;WITH T AS ( AS BIGINT) AS curr, CAST(NULL AS BIGINT) AS prv UNIO ...
- [转]HTML字符实体(Character Entities),转义字符串(Escape Sequence)
为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...
- uva 10817(数位dp)
uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...
- python3 求斐波那契数列(Fibonacci sequence)
输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列( ...
随机推荐
- Python2.7.3 学习——准备开发环境
安装环境搭建参考:http://blog.163.com/sunshine_linting/blog/static/4489332320129187464222/ 第一种方式,通过命令行方式安装Pyt ...
- Python 中psutil 模块的安装
第一步下载psutil 的安装包 网址:https://pypi.python.org 第二步解压 .tar.gz cd psutil- 第三步安装: python setup.py build py ...
- RedHat/CentOS发行版本号及内核版本号对照表
RedHat/CentOS发行版本号及内核版本号对照表 : Redhat 9.0———————————————2.4.20-8RHEL 3 Update 8————————————2.4.21-47R ...
- swig模板 html代码自然状态下输出是转义的,必须加一个函数来转换为html代码;
<div>{{o.content|raw}}</div> |raw 相当于一个函数,转义函数,最终输出结果为html代码:
- MyMVC框架的使用
1)在web.config 中system.web 节点下加入例如以下代码 <pages controlRenderingCompatibilityVersion="4.0" ...
- HDU 1051 Wooden Sticks (贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- BZOJ 1012 最大数maxnumber
Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...
- POJ 3268 Silver Cow Party 正反图最短路
题目:click here 题意: 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都 ...
- Java 网络编程(六) 使用无连接的数据报(UDP)进行通信
连接地址:http://www.cnblogs.com/mengdd/archive/2013/03/10/2952673.html 使用无连接的数据报(UDP)进行通信 什么是Datagram? 数 ...
- servlet三种实现方式之二继承GenericServlet开发
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第二种示例代码如下(已去掉包名): //这是第二 ...