题目:

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. liunx使用技巧

    1.挂载与卸载U盘 新建一个目录:mkdir /mnt/usb; Fdisk –l |less  查看添加之后的设备名,设备文件系统格式 加载U盘设备: mount –t vfat /mnt/usb ...

  2. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  3. AIDE支持实时错误检查、代码重构、代码智能导航、生成APK

    AIDE是一个Android Java集成开发环境,可以在Android系统内进行Android软件和游戏的开发.它不仅仅是一个编辑器,而是支持编写-编译-调试运行整个周期,开发人员可以在Androi ...

  4. 51nod 1244 莫比乌斯函数之和(杜教筛)

    [题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...

  5. 有道翻译API

    轻奢侈品_百度百科 轻奢侈品 有道翻译API 有道翻译API申请成功 API key:72763558 keyfrom:lexus-studio

  6. java的数学函数总结

    java的数学函数都放在java.lang这个包中,并且这些函数的方法在类Math中是作为static方法出现的,所以要引用一个特定的函数,只需将类Math和一个圆点写在要使用的方法前就好.如方法sq ...

  7. To the Max(矩阵压缩)

    To the Max Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Su ...

  8. getDeclaredConstructor()与getConstructor的差别

    首先看getDeclaredConstructor(Class<?>... parameterTypes)  这种方法会返回制定參数类型的全部构造器,包含public的和非public的, ...

  9. CodeForces 22C System Administrator

    把v和2结点交换, 1和v连,其它点和v之间能够互相连. #include <iostream> #include <cstdlib> #include <cstring ...

  10. JavaWeb学习过程 之c3p0的使用

    这几天在学习使用MVC模式来做几个小项目,在学习的过程中,用到了数据库连接池.便特意去学习了一下. 一.谈一谈为什么要使用数据库连接池 在开发基于数据库的web程序时,传统的模式(在servlet,b ...