UVa-1594

学到了新姿势,对结构体用map,set,sort等,可以在结构体中重载小于号(也有别的方式,没有仔细研究。)。

注意const偷懒不加的话会通不过编译。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<set>
 6 using namespace std;
 7 const int maxx=20;
 8 struct node
 9 {
10     int a[maxx],n;
11     bool operator<(const struct node &x) const
12     {
13         for(int i=1;i<=n;i++)
14             if(a[i]!=x.a[i]) return a[i]<x.a[i];
15     }
16 };
17 int main()
18 {
19     //freopen("in.txt","r",stdin);
20     //freopen("out.txt","w",stdout);
21     int T;
22     scanf("%d",&T);
23     while(T--)
24     {
25         int n;
26         scanf("%d",&n);
27         bool flag=1;
28         set<struct node> st;
29         struct node anode;
30         anode.n=n;
31         for(int i=1;i<=n;i++) anode.a[i]=0;
32         st.insert(anode);
33         for(int i=1;i<=n;i++) scanf("%d",&anode.a[i]);
34         if(st.find(anode)!=st.end()) flag=1;
35         else
36         {
37             st.insert(anode);
38             while(1)
39             {
40                 int t=anode.a[1];
41                 for(int i=1;i<n;i++)
42                     anode.a[i]=abs(anode.a[i]-anode.a[i+1]);
43                 anode.a[n]=abs(anode.a[n]-t);
44                 set<struct node>::iterator it=st.find(anode);
45                 if(it!=st.end())
46                 {
47                     if(it==st.begin()) flag=1;
48                     else flag=0;
49                     break;
50                 }
51                 st.insert(anode);
52             }
53         }
54         puts(flag?"ZERO":"LOOP");
55     }
56 }
原文地址:https://www.cnblogs.com/windrises/p/4653150.html