zoukankan      html  css  js  c++  java
  • 算法习题---5-2Ducci序列(UVa1594)

    一:题目

    对于一个n元组(a1, a2, …, an),可以对于每个数求出它和下一个数的差的绝对值,得到一个新的n元组(|a1-a2|, |a2-a3|, …, |an-a1|)。重复这个过程,得到的序列称为Ducci序列,例如: 
    (8, 11, 2, 7) -> (3, 9, 5, 1) -> (6, 4, 4, 2) -> (2, 0, 2, 4) -> (2, 2, 2, 2) -> (0, 0, 0, 0). 
    也有的Ducci序列最终会循环。输入n元组(3≤n≤15),你的任务是判断它最终会变成0还是会循环。输入保证最多1000步就会变成0或者循环。

    (一)样例输入

    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

    (二)样例输出

    ZERO
    LOOP
    ZERO
    LOOP

    二:代码实现

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    #define MAX_N 15
    
    int main()
    {
        freopen("data5_2_h.in", "r", stdin);
        freopen("data5_2_h.out", "w", stdout);
        
        int n,m,k,a[MAX_N];
        cin >> n;
    
        for (int i = 0; i < n; i++)
        {
            //获取一组信息
            cin >> m;
            for (int j = 0; j < m;j++)
                cin >> a[j];
            //进行信息处理
            for (k = 0; k < 1000; k++)    
            {
                int val,count=0,f_ele = a[0];
                for (int j = 0; j < m; j++)
                {
                    if (j == m - 1)
                        val = a[j] - f_ele;
                    else
                        val = a[j] - a[j + 1];
                    val > 0 ? a[j] = val : a[j] = -val;
                    if (a[j] == 0)
                        count++;
                }
                if (count==m)
                    break;
            }
            k == 1000 ? cout << "LOOP
    " : cout << "ZERO
    ";
        }
    
        freopen("CON", "r", stdin);
        freopen("CON", "w", stdout);
        return 0;
    }
  • 相关阅读:
    for...in 循环对象原型链问题
    移动端表单禁用软键盘
    将一个普通数组映射为对象数组
    npm install命令详解
    Elasticsearch High Level REST Client
    Guava: Google Core Libraries for Java
    Java Interview Programming Questions
    2017 OWASP TOP 10
    17 Popular Java Frameworks in 2018
    10 Popular PHP frameworks in 2019
  • 原文地址:https://www.cnblogs.com/ssyfj/p/11539554.html
Copyright © 2011-2022 走看看