UVA 1594 Ducci序列(Ducci Sequence, ACM/ICPC Seoul 2009)

紫书 第五章 习题5-2
解法:暴力模拟即可。
AC代码:(390ms)

#include
#include
#include
#include
#pragma warning(disable:4996)
using namespace std;
set<vector<short>>s; vector<short> v;
int main() {
	unsigned int t; scanf("%u", &t); int n, n0; short a; unsigned char allzero, exitfor;
	for (unsigned int i = 0; i < t; ++i) {
		scanf("%d", &n); s.clear(); v.clear();
		for (int j = 0; j < n; ++j) {
			scanf("%hd", &a); v.emplace_back(a);
		}
		s.emplace(v); exitfor = 0;
		for (; exitfor == 0;) {
			n0 = n - 1; a = v[0]; allzero = 1; 
			for (int j = 0; j < n0; ++j) {
				v[j] = abs(v[j] - v[j + 1]); if (v[j] != 0)allzero = 0;
			}
			v[n0] = abs(v[n0] - a); if (v[n0] != 0)allzero = 0;
			switch (allzero) {
			case 1:puts("ZERO"); exitfor = 1; continue;
			case 0:
				switch (s.count(v)) {
				case 1:puts("LOOP"); exitfor = 1; continue;
				case 0:s.emplace(v);
				}
			}
		}
	}
	//system("pause"); 
	return 0;
}

你可能感兴趣的:(ACM-ICPC)