zoukankan      html  css  js  c++  java
  • UVa 1594

    题意

    对于一个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或者循环。

    思路

    用vector< int>存Ducci序列, 用set< vector< int> > 存这个更新过的Ducci序列是否陷入死循环
    vector可以直接用 ” == ” 比较是否相等, 故每次新建一个长度为n的0vector
    记得先保存一下s[0], 因为在对s[n-1]操作的时候用到的s[0]已经被改变了!!

    AC代码

    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <set>
    
    using namespace std;
    
    vector<int> s, zero;
    typedef vector<int> v;
    set<v> Set;
    
    int main()
    {
        int T, n, i, x;
        cin >> T;
        while(T--){
            cin >> n;
            for( i = 0; i < n; i++ )
                zero.push_back(0);
            for( i = 0; i < n; i++ ){
                cin >> x;
                s.push_back(x);
            }
            if(s == zero){
                cout << "ZERO" << endl;
                if( !s.empty() )    s.clear();
                if( !zero.empty() )     zero.empty();
                continue;
            }
            for(;;){
                int x = s[0];
                for( i = 0; i < n; i++ ){
                    if(i != n-1)  s[i] = abs(s[i]-s[i+1]);
                    else s[i] = abs(s[i]-x);
                }
                if(s == zero){
                    cout << "ZERO" << endl;
                    break;
                }
                else if( Set.count(s) ){
                    cout << "LOOP" << endl;
                    break;
                }
                Set.insert(s);
           }
            if( !s.empty() )    s.clear();
            if( !zero.empty() )     zero.clear();
            if( !Set.empty() )   Set.clear();
        }
        return 0;
    }
  • 相关阅读:
    jQuery初学:find()方法及children方法的区别分析
    百万级访问网站前期的技术准备
    TCP/IP协议三次握手与四次握手流程解析
    TCP/IP详解学习笔记
    Dubbo框架入门介绍
    如何提高Web服务端并发效率的异步编程技术
    杂 -- 有关程序员
    关于高性能的那点事
    大型网站的灵魂- 性能
    分布式java应用
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740627.html
Copyright © 2011-2022 走看看