Ducci Sequence

Description

 

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(3n15), 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
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 开始想着用 队列 做,然后用 map 判断是否重复 , 然而 出现问题了, 猜测应该是在map中查找重复数列 有问题,代码如下
#include <iostream>
#include <string>
#include <queue>
#include <cmath>
#include <map> //vector判断是否重复
using namespace std; typedef queue<int> Queue;
map<Queue,int> loop;
queue<int> ling;
int main()
{
int T;
cin >> T;
while(T--){
int n, a;
queue<int> ducci;
cin >> n;
for(int i = ;i < n; i++)ling.push();
loop[ling] = ;
for(int i = ;i < n; i++){
cin >> a;
ducci.push(a);
}
int time = ;
bool flag = false;
while(++time){
cout<<"----------------------------"<<endl;
cout<<time<<endl;
a = ducci.front();
cout<<a;
ducci.pop();
int start = a,t;
loop[ducci] = ;
for(int i = ;i < n; i++){
t = ducci.front();
cout<<"--"<<t;
ducci.pop();
ducci.push(abs(a-t));
a = t;
}
ducci.push(abs(start-t));
cout<<endl;
for(int i = ;i < time;i++){
cout<< "i "<<i<<endl;
if(loop[ducci]!=){
if(i == )cout << "ZERO" << endl;
else cout << "LOOP" <<endl;
flag = true;
break;
} }
if(flag)break;
}
}
// system("pause");
return ;
}

然后 用数组做吧
用map判定老出现问题,然后就真的不会用stl做了

#include <iostream>
#include <cmath>
using namespace std;
int ducci[][];
int main()
{
int T;
cin >> T;
while(T--){
int n;
cin >> n;
for(int i = ;i < n; i++)ducci[][i] = ;
for(int i = ;i < n; i++)cin >> ducci[][i];
int time = ;
bool flag = false;
while(++time){
//cout<<"----------------------------"<<endl;
//cout<<time<<endl;
int t;
for(int i = ;i < n - ; i++){
ducci[time][i] = abs(ducci[time-][i+] - ducci[time-][i]);
//cout<<ducci[time][i]<<"--";
}
ducci[time][n-] = abs(ducci[time-][] - ducci[time-][n-]);
//cout<<ducci[time][n-1]<<endl;
for(int i = ;i < time;i++){
int flag1 = ;
for(int j = ;j < n;j++){
if(ducci[time][j] != ducci[i][j]){
flag1 = ;
break;
}
}
if(flag1){
if(i == )cout << "ZERO" << endl;
else cout << "LOOP" <<endl;
flag = true;
break;
} }
if(flag)break;
}
}
// system("pause");
return ;
}

还是太不熟悉stl了
看别人的代码用stl做的

结构体 重载运算符什么的  都不熟唉

#include <cstdio>
#include <cstring>
#include <map> //map判断重复
#include <cmath>
#include <algorithm>
using namespace std; struct Node{
int a[];
int n;
void read() {
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
}
void ducci() {
int tmp = a[];
for (int i = ; i < n-; i++) {
a[i] = abs(a[i]-a[i+]);
}
a[n-] = abs(a[n-]-tmp);
} bool operator <(const Node &b) const {
for (int i = ; i < n; i++) {
if (a[i] != b.a[i]) return a[i]<b.a[i];
}
return false;
}
bool iszero() {
for (int i = ; i < n; i++) {
if (a[i] != ) return false;
}
return true;
}
}lala; map<Node, bool>vis; int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &lala.n);
lala.read();
vis.clear();
vis[lala] = true;
bool isloop = false;
for (int i = ; i < ; i++) {
lala.ducci();
if (vis[lala]) {
isloop = true;
break;
}
vis[lala] = true;
} if (isloop && !lala.iszero()) puts("LOOP");
else puts("ZERO");
}
return ;
}

还是强

uva 1594 Ducci Sequence <queue,map>的更多相关文章

  1. UVA 1594 Ducci Sequence(两极问题)

           Ducci Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   D ...

  2. 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, · · · ...

  3. Uva - 1594 - Ducci Sequence

    水题,算出每次的结果,比较是否全0,循环1000次还不是全0则LOOP AC代码: #include <iostream> #include <cstdio> #include ...

  4. 【暴力模拟】UVA 1594 - Ducci Sequence

    想麻烦了.这题真的那么水啊..直接暴力模拟,1000次(看了网上的200次就能A)后判断是否全为0,否则就是LOOP: #include <iostream> #include <s ...

  5. 【UVA】1594 Ducci Sequence(纯模拟)

    题目 题目     分析 真的快疯了,中午交了一题WA了好久,最后发现最后一个数据不能加\n,于是这次学乖了,最后一组不输出\n,于是WA了好几发,最后从Udebug发现最后一组是要输出的!!!   ...

  6. UVa----------1594(Ducci Sequence)

    题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...

  7. Ducci Sequence UVA - 1594

      A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...

  8. LIS UVA 10534 Wavio Sequence

    题目传送门 题意:找对称的,形如:123454321 子序列的最长长度 分析:LIS的nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理.因为是对称的,所以取较小值*2-1 ...

  9. uva 10534 Wavio Sequence LIS

    // uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...

随机推荐

  1. with 与 debugger

    with在严格模式下是禁止使用的,而debugger是在调试模式下才有效果的,目测作者自己在用的脚本压缩工具在有dubugger语句的情况下会影响压缩结果,导致失败. with(varible)实际上 ...

  2. oracle 10g RAC psu过程

    1 升级crs 至10.2.0.5.2 1) 升级opatch 程序,PSU对opatch的版本有要求,详见readme文件,此步操作共涉及到每个节点的ORACLE_HOME和ORA_CRS_HOME ...

  3. python学习第三天 --布尔类型

    我们已经了解了Python支持布尔类型的数据,布尔类型只有True和False两种值,但是布尔类型有以下几种运算: 与运算:只有两个布尔值都为 True 时,计算结果才为 True. True and ...

  4. IOS 开发之文件管理

    一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库 ...

  5. android 利用Bitmap获取圆角矩形、圆形图片

    1.在很多时候,我们要显示图片资源,需要将他的资源显示为圆角的:示例源码如下: public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,fl ...

  6. AngularJS 基础教程一:

    一:了解AngularJS AngularJS是一款非常优秀的前端高级 JS 框架,由 Misko Hevery 等人创建 2009 年被 Google 收购,用于其多款产品 有一个全职的开发团队继续 ...

  7. bzoj1004 Cards

    Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给出了答案.进一步,小春要求染出Sr张红 ...

  8. 【HDU1166】敌兵布阵(树状数组或线段树)

    是一道树状数组的裸题,也可以说是线段树的对于单点维护的裸题.多做这种题目可以提高自己对基础知识的理解程度,很经典. #include <iostream> #include <cst ...

  9. javascript运算符整理

    说起运算符,基本上各类编程语言中都会涉及,使用方法大同小异.今天在这里以javascript做简单的整理. 总得来说运算符还是比较的多,大致可以分为以下几种类型: 一元运算符 位运算符 布尔运算符 乘 ...

  10. JAX-WS 可运行项目

    该项目是通过JAX-WS实现的WebService服务,其中包括了1.关于最简单的WebService服务的创建2.关于文件交互的WebService的创建 代码中包括了服务端代码和客户端代码(客户端 ...