2015 HUAS Summer Training#1 B

题目:

A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1a2, ... an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers:

a1a2... 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.

题目大意:给你N个数,用( a1a2... an (| a1 - a2|,| a2 - a3|, ... ,| an - a1|)在1000次计算里找它的是否循环 是输出LOOP 否输出ZERO;

解题思路:用vector数组存储这些数  在进行比较;

代码:

 1 #include<iostream>
 2 #include<math.h>
 3 #include<vector>
 4 using namespace std;
 5 const  int l=1000+10;
 6
 7 int main()
 8 {
 9     vector<int> a[l];
10     vector<int> b;
11     int    T,n,i,t,j,d,p;
12     cin>>T;
13     while(T--)
14     {
15         cin>>n;
16         if(n>=3&&n<=15)
17         {
18             d=0;
19             b.resize(n);
20             for(i=0;i<l;i++)
21                 a[i].resize(n);
22             for( i=0;i<n;i++)
23             {
24                 scanf("%d",&a[0][i]);
25                 b[i]=0;
26                 if(a[0][i]<0||a[0][i]>1000)
27                     scanf("%d",&a[0][i]);
28             }
29             for(i=0;i<1000;i++)
30             {
31                 t=a[i][0];
32                 for(j=0;j<n;j++)
33                 {
34                     if(j==(n-1))
35                         a[i+1][j]=abs(a[i][n-1]-t);
36                     else
37                         a[i+1][j]=abs(a[i][j]-a[i][j+1]);
38                 }
39                 if(a[i+1]==b)
40                 {
41                     printf("ZERO\n");
42                     d=i;
43                     break;
44                 }
45             }
46             if(d==0)
47                 printf("LOOP\n");
48         }
49     }
50     return 0;
时间: 2024-08-03 07:09:14

2015 HUAS Summer Training#1 B的相关文章

2015 HUAS Summer Training#2~C

Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数...,以后从头开始轮流进行一至二报数.一至三报数直到剩下的人数不超过三人为止. Input 本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000. Output 共有N行,分别对应输入的新兵人数,每行输出剩下

2015 HUAS Summer Training#2 F

题目: Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j) denotes the cell in row i and column j in the

2015 HUAS Summer Training#2 B

题目: Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of card

2015 HUAS Summer Training#2 G

题目: Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Far

2015 HUAS Summer Training#2 D

题目: Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / / B E / \ / \ \ A C G / /

2015 HUAS Summer Training#2 A

题目: You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, AB is correct, (c) if A is correct, (A ) and [A ] is correct. Write a program that

2015 HUAS Summer Training#1~D

10763 Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assist

2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North America - Rocky Mountain 开始时貌似是UVAlive挂了,无论交什么都WA,后来转战HDU 这次水题比较多,其中B题据说有更加高级的方法. G题WA了两发,才发现竟然没有输出Case!!! 未完成:D F H I J A - Iterated Difference 水题,模拟迭代即可

2015 UESTC Winter Training #6【Regionals 2010 &gt;&gt; North America - Rocky Mountain】

2015 UESTC Winter Training #6 Regionals 2010 >> North America - Rocky Mountain A - Parenthesis 给一个长度不多于1000的表达式,只包含小写字母,加法运算,省略乘号的乘法运算和括号,输出去掉多余括号的表达式 括号匹配可以使用栈操作,只有两种情况可以去掉这一对括号: 左括号的左边是左边界.加法符号.左括号,并且右括号右边是有右边界.加法符号.右括号 如果括号内没有加法运算(括号内的括号里,也就是下一级括