简单题 应用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
杜西序列求解
本文介绍了一种使用C++实现的杜西序列求解方法,通过定义自定义结构体并重载比较运算符来实现对整数序列的操作,并利用集合记录序列状态,最终判断序列是否进入循环或全为零。
2369

被折叠的 条评论
为什么被折叠?



