题目:

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)的更多相关文章

  1. UVA 1594:Ducci Sequence (模拟 Grade E)

    题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...

  2. 【每天一题ACM】 斐波那契数列(Fibonacci sequence)的实现

    最近因为一些原因需要接触一些ACM的东西,想想写个blog当作笔记吧!同时也给有需要的人一些参考 话不多说,关于斐波那契数列(Fibonacci sequence)不了解的同学可以看看百度百科之类的, ...

  3. HTML字符实体(Character Entities),转义字符串(Escape Sequence)

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  4. HTML字符实体(Character Entities),转义字符串(Escape Sequence)【转】

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  5. HTML字符实体(Character Entities),转义字符串(Escape Sequence) 转

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  6. SQL Server ->> 斐波那契数列(Fibonacci sequence)

    斐波那契数列(Fibonacci sequence)的T-SQL实现 ;WITH T AS ( AS BIGINT) AS curr, CAST(NULL AS BIGINT) AS prv UNIO ...

  7. [转]HTML字符实体(Character Entities),转义字符串(Escape Sequence)

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  8. uva 10817(数位dp)

    uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...

  9. python3 求斐波那契数列(Fibonacci sequence)

    输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列( ...

随机推荐

  1. 闪存主控IC的作用

    闪存主要是由闪存芯片.主控芯片.晶振.PCB板等部件组成的.其中主控芯片相当于闪存的“灵魂”,它控制着闪存的工作.主控芯片也是处理单元,在里面写入的程序对整个电路做控制.主控IC是把flash跟hos ...

  2. eclipse及Java常用问题及解决办法汇总

    junit-test 我觉得这点比idea好用,可以直接选中要测试的方法名,右击run as即可 http://www.cnblogs.com/brolanda/p/4532779.html 打开您的 ...

  3. C++读写EXCEL文件OLE,java读写excel文件POI 对比

    C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...

  4. HDU 5820 Lights(扫描线+zkw线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5820 [题目大意] 在一个大小为50000*50000的矩形中,有n个路灯. 询问是否每一对路灯之 ...

  5. SQLServer 2008 :error 40 出现连接错误

      在与SQLServer建立连接时出现与网络相关的或特定与实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且SQL SERVER已配置允许远程链接.(provide:命名管道提供程序,e ...

  6. IronJs 无相关源?

    在工作中用到了IronJs  一切正常 就是 在启动项目的时候 报错说无相关源,将取消引用之后 就可以正常运行 如果不取消引用需要重新启动~ 是这个样子滴~ 说明: 在编译向该请求提供服务所需资源的过 ...

  7. 视频媒体播放,最好的 HTML 解决方法

    最好的 HTML 解决方法 HTML 5 + <object> + <embed> <video width="320" height="2 ...

  8. iOS 从app跳转到Safari、从app打开电话呼叫

    1.从app跳转到Safari NSString* strIdentifier = @"http://www.ybyb.com"; BOOL isExsit = [[UIAppli ...

  9. C实现二叉树(模块化集成,遍历的递归与非递归实现)

    C实现二叉树模块化集成 实验源码介绍(源代码的总体介绍):header.h : 头文件链栈,循环队列,二叉树的结构声明和相关函数的声明.LinkStack.c : 链栈的相关操作函数定义.Queue. ...

  10. 如何将 MFC ActiveX 控件标记为安全,脚本和初始化

    MSDN原文.ActiveX控件标记安全(可以不仅仅是MFC ActiveX) 概要 默认情况下,MFC ActiveX 控件未标记为对脚本编写是安全的和对初始化是安全的.控制运行在 Internet ...