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. vector -1

    vector的特色有支持随机存取,在集合尾端增删元素很快,但是在集合中间增删元素比较费时. vector以模板(泛型)方式实现,可以保存任意类型的变数,包括使用者自定义的资料型态,例如:它可以是放置整 ...

  2. c#打开指定设备程序以及网址

    //打开计算器 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"C:\WINDOWS\ ...

  3. C# ITextShap 生成PDF 下载

    using iTextSharp.text; using iTextSharp.text.pdf; //创建 Document Document pdfDoc = new Document(new R ...

  4. Mysql int(11) 和 int(1)

    Mysql 可以为整数类型制定宽度,例如:int(11) ,对大多数应用这是没有意义的:它不会限制值的合法范围,它只是规定了Mysql的一些交互工具(例如mysql命令行客户端)用来显示字符个数.对于 ...

  5. activiti任务TASK

    一.概要 设计TASK的表主要是:ACT_RU_TASK,ACT_HI_TASKINST(见参考-activiti表): 任务主要有:人工任务(usertask),服务任务(servicetask)等 ...

  6. 小安,今天学会了MySQL中查询时间的方法哦

  7. WordPress插件制作教程(五): 创建新的数据表

    上一篇讲解了怎样将数据保存到数据库,今天为大家讲解创建新的数据表,也就是说当我们激活插件的时候,会在该数据库下面创建一个新的数据表出来.原理很简单,激活插件的时候运行创建数据库的代码.看下面代码: & ...

  8. JDK PATH 和 CLASSPATH环境变量的作用及其配置

    (1)PATH环境变量的作用 在安装JDK程序之后,在安装目录下的bin目录中会提供一些开发Java程序时必备的工具程序. 对于Java的初学者,建议在命令符模式下使用这些工具程序编译运行Java程序 ...

  9. 开心菜鸟学习系列笔记-----Javascript(1)

    js 一些常见的使用方法        // target : 不管是否出现冒泡,他都是代表最开始引发事件的对象   // this   : 是指当前函数.  //ie 事件对象   : window ...

  10. adnroid 监听收到的短信并根据短信内容进行回复短信

    定义一个广播接收器 public class SMSReceiver extends BroadcastReceiver { private SmsManager smsManager; @Overr ...