uva 1594Ducci Sequence

来源:互联网 发布:httpclient java 编辑:程序博客网 时间:2024/05/17 00:49

原题:
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a 1 ,a 2 ,···,a n ), the
next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers:
(a 1 ,a 2 ,···,a n ) → (|a 1 − a 2 |,|a 2 − a 3 |,···,|a n − a 1 |)
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.
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
中文:
紫书上的练习题,给你n个数,这n个数可以分别和下一个数相减,如下(a 1 ,a 2 ,···,a n ) → (|a 1 − a 2 |,|a 2 − a 3 |,···,|a n − a 1 |)。如果,不断进行这样的操作,如果出现全是0输出ZERO,如果出现循环输出LOOP。数据保证出现循环或者0

#include <bits/stdc++.h>using namespace std;set<vector<int>> sv;int main(){    ios::sync_with_stdio(false);    int t,n;    cin>>t;    while(t--)    {        cin>>n;        vector<int> v,emp(n,0);        sv.clear();        for(int i=0;i<n;i++)        {            int x;            cin>>x;            v.push_back(x);        }        sv.insert(v);        int flag=0;        vector<int> tmp;        while(true)        {            tmp.clear();            for(int i=0;i<n-1;i++)                tmp.push_back(abs(v[i+1]-v[i]));            tmp.push_back(abs(v[n-1]-v[0]));            v=tmp;            if(sv.find(emp)!=sv.end())            {                flag=1;                break;            }            if(sv.find(tmp)!=sv.end())            {                flag=2;                break;            }            sv.insert(tmp);        }        if(flag==1)            cout<<"ZERO"<<endl;        else            cout<<"LOOP"<<endl;    }    return 0;}

解答:
紫书上的练习题,题目很简单,利用vector重载好的一系列操作保存多个值,然后利用set判断是否有重复即可。

0 0
原创粉丝点击