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. I Love You Too HDU 2816

    Description This is a true story. A man showed his love to a girl,but the girl didn't replied clearl ...

  2. Java Load Properties 文件,定义message信息

    初始化Properties对象,load properties文件: private static final Properties MESSAGERESOURCES = new Properties ...

  3. HTML5视频

    <video>标签用于定义视频. 案例1: <!DOCTYPE html><html><head lang="en"> <me ...

  4. php+mssql 已经写好的万能函数

    <?php /****************************************************************************************** ...

  5. 换行word-wrap与word-break兼容IE和FIREFOX -----设计师零张

    word-wrap是控制换行的.使用break-word时,是将强制换行.中文没有任何问题,英文语句也没问题.但是对于长串的英文,就不起作用.word-break是控制是否断词的.normal是默认情 ...

  6. Python核心编程(第九章)--文件和输入输出

    文件内建函数: open()函数提供了初始化输入/输出操作的通用接口 open()基本语法:file_object = open(filename,access_mode='r',buffering= ...

  7. 【5】python核心编程 第八章-条件和循环

    1.=== range() 的完整语法=== Python 提供了两种不同的方法来调用 range() . 完整语法要求提供两个或三个整数参数: range(start, end, step =1) ...

  8. Tornado web 框架

    Tornado web 框架 其实很简单.深度应用 一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像we ...

  9. shell脚本的桩

    项目代码: alias book_search="/usr/local/mysql/bin/mysql -h 172.18.12.202 -uppstat -pstatpp book_sea ...

  10. Source Insight设置总结

    在网上搜索了一些关于Source Insight的设置技巧,把这些结果给总结下来: 1. 背景色选择 要改变背景色Options->preference->windows backgrou ...