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. 路由转发(curl)

    <?php ini_set('memory_limit', '640M'); ini_set('default_charset', 'utf-8'); define('webroot', 'ht ...

  2. JAVA 对象引用,以及对象赋值(转)

    原文链接:http://zwmf.iteye.com/blog/1738574 关键字: java对象 引用 Java对象及其引用 关于对象与引用之间的一些基本概念. 初学Java时,在很长一段时间里 ...

  3. BZOJ 2330 SCOI 2011 糖果

    2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友 ...

  4. MYSQL常用简单语句

    使用SQL语法大写,增加可读性(小写部分就是自己数据库写的表/字段喽,具体你懂得...). 创建数据库:CREATE DATABASE mysql_db;删除数据库:DROP DATABASE mys ...

  5. php+mysql将大数据sql文件导入数据库

    <?php $file_name = "d:test.sql"; $dbhost = "localhost"; $dbuser = "root& ...

  6. django的model对象转化成dict

    今天发现一个掉渣天的方法,Django的forms包里面有一个方法:model_to_dict(),它可以将一个model对象转化成dict. In [1]: from apps.dormitory. ...

  7. php跨服务器传递对象

    最近因为研究跨域名,跨服务器的问题,所以无聊,就想到了一个跨服务器传递对象的问题. 想要跨服务器传递数据,那么就要使用到get或者post提交. 我这里的方法有点复杂,但是因为平时工作时,有封装相应的 ...

  8. Oracle BigFile

    http://blog.chinaunix.net/uid-20779720-id-3078273.html

  9. BLOB二进制对象(blob.c/h)

    BLOB二进制对象(blob.c/h) 数据结构 struct blob_attr { uint32_t id_len; /** 高1位为extend标志,高7位存储id, * 低24位存储data的 ...

  10. Hdu3640-I, zombie(模拟+二分)

    The "endless" model in "I, zombie" of "Plants vs. Zombies" is my favou ...