zoukankan      html  css  js  c++  java
  • UVA

    给定n元组(a1,a2,...,an),ai均为整数,得到下一个序列为(|a1-a2|,|a2-a3|,...,|an-a1|),如此循环下去,必定会出现全零序列或重复序列

    现要求判断给定序列是全零序列还是重复序列(有趣的Ducii结果)

    思路分析

    用vector模拟元组序列,set<vector<int>>用于元组序列判重,完成重复序列发现

    #include<bits/stdc++.h>
    using namespace std;
    int main() {
        freopen("in.txt", "r", stdin);
        int t, n;
        cin >> t;
        while (t--) {
            cin >> n;
            vector<int>a(n), b(n), zero(n, 0);
            for (int i = 0; i < n; ++i)  cin >> a[i];
            set<vector<int>>_set;
            int flag = 0;
            while (!flag) {
                if (a == zero) flag = 1;
                else if (_set.find(a) != _set.end()) flag = 2;
                else {
                    _set.insert(a);
                    for (int i = 0; i < n; i++) b[i] = abs(a[i] - a[(i + 1) % n]); // ducci下一个序列
                    a = b;
                }
            } 
            cout << (flag == 1 ? "ZERO
    " : "LOOP
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    第十九天:类和对象
    第十五天:模块
    十四天:匿名函数
    十四天作业
    第十三天:迭代器、递归
    十二天:闭包和装饰器
    一个炒鸡简单的购物车
    十一天
    第十天
    第十天作业
  • 原文地址:https://www.cnblogs.com/RioTian/p/13021058.html
Copyright © 2011-2022 走看看