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 # ----斐波那契数列( ...
随机推荐
- python读取word表格内容(1)
1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格
- C# async await 例子
private static async void Worker() { Console.Write("main thread id is :{0}",Thread.Current ...
- GLView基本分析
GLView是cocos2d-x基于OpenGL ES的调用封装UI库. OpenGL本身是跨平台的计算机图形实现API,在每一个平台的详细实现是不一样.所以每次使用之前先要初始化,去设置平台相关的信 ...
- Android UI设计
Android UI设计--PopupWindow显示位置设置 摘要: 当点击某个按钮并弹出PopupWindow时,PopupWindow左下角默认与按钮对齐,但是如果PopupWindow是下图的 ...
- iperf
iperf命令是一个网络性能测试工具.iperf可以测试TCP和UDP带宽质量.iperf可以测量最大TCP带宽,具有多种参数和UDP特性.iperf可以报告带宽,延迟抖动和数据包丢失.利用iperf ...
- /etc/ld.so.conf详解
/etc/ld.so.conf 此文件记录了编译时使用的动态库的路径,也就是加载so库的路径. 默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件,而通常通过源码包进行安装 ...
- 初步认识ExtJS
最近因为项目,需要去学习ExtJS的相关内容. 现在对于ExtJS完全是小白一枚. 目前使用的是ExtJS4.2的版本,官网上现在最新版本是6的. 第一个方法:Ext.onReady() Ext.on ...
- asp.net 分页的制作
/// <summary> /// 数据分页方法 /// </summary> /// <param name="PageIndex">当前页& ...
- Eclipse怎么全局搜索替换(整个项目)
链接地址:http://jingyan.baidu.com/article/3ea51489c1c0d752e61bba2e.html 我们用Eclipse编程,有时候需要将整个项目的某个字符串替换成 ...
- BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )
dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) + G[ i ][ j ] ( i - 1 <= k <= i + 1 , dp[ k ][ j - ...