ACM ICPC 2009 Asia Seoul Ducci Sequence 题解

暴力一手。

对于一个数列,我们模拟 (500) 次,如果没有全变为零就代表 LOOP 了。

时间复杂度 (O(T imes 500 imes N))

#include <bits/stdc++.h>
using namespace std;
int t,n,a[20],b[20];
bool check(int ll[]) {
	for(int i=0;i<n;i++)
		if(ll[i])
			return 0;
	return 1;
}
int main() {
	scanf("%d",&t);
	for(;t;--t) {
		bool sum=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(int i=1;i<=500;i++) {
			for(int j=1;j<n;j++)
				b[j]=abs(a[j]-a[j+1]);
			b[n]=abs(a[1]-a[n]);//进行模拟
			if(check(b)) {
				puts("ZERO");
				sum=1;
				break;
			}
			memcpy(a,b,sizeof a);//复制到 a 数组
		}
		if(!sum)
			puts("LOOP");
	}
}
原文地址:https://www.cnblogs.com/lajiccf/p/12942807.html