UVa 1594 解题报告

来源:互联网 发布:网络是怎样连接的 编辑:程序博客网 时间:2024/06/04 22:16

简单题 应用set 一遍AC

#include <bits/stdc++.h>

using namespace std;
struct ducci
{
    vector<int> v;
    bool operator < (const ducci & b)const{
        for (int i = 0; i < v.size(); ++i)
            if(v[i]<b.v[i])return v[i] < b.v[i];
        return false;
    }//自己定义小于运算符
};
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    cin >> T;
    while(T--){
        int tot,cnt,ok,ans=1;
        cin >> tot;
        ducci d;
        set<ducci> s;
        for (int i = 0; i < tot; ++i){
            int x;
             cin >> x;
             d.v.push_back(x);
        }
        s.insert(d);
        while(1){
            int len = d.v.size();
            ok=1;
            for(int i=0;i<len;i++){//全是0 退出循环
                if(d.v[i]!=0){ok=0;break;}
            }
            if(ok)break;
            int x=d.v[0];//注意要先保存第一个数,改了再用会出错
            for (int i = 0; i < len-1; ++i)
            {
                d.v[i]=abs(d.v[i]-d.v[i+1]);
                //cout << d.v[i] << " ";
            }
            d.v[len-1] = abs(d.v[len-1] - x);
            //cout << d.v[len-1] << endl;
            s.insert(d);
            if(s.size()==ans){ok=0;break;}//集合中个数不变视为找到循环退出
            else ans++;
        }
        if(ok) cout << "ZERO" << endl;
        else cout << "LOOP" << endl;
    }
    return 0;
}

原题如下:

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