UVA 1594
来源:互联网 发布:最优化理论 推荐教材 编辑:程序博客网 时间:2024/06/07 05:03
题意:有一个n元组,变化到下一个状态时a[i]为上一个状态的a[i+1]-a[i](i=n时为a[1]-a[n]),一直变化问会循环还是变成n个0,最多一千步就能得到答案。3<=N<=15。
思路:可以考虑用map判重,循环一千次,如果当前状态重复了或者为全零就跳出循环输出。用struct类型作map的key时要重载<号。
(这里说要么会循环,要么会变为全零,也可以循环一千步,只判零,如果最后没有出现全零那一定是循环,这样写起来会简单一些)
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <map>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod %100000007struct node{ int a[20]; int tot; void change() { int temp = a[1]; rep(i,1,tot-1) a[i] = abs( a[i+1] - a[i] ); a[tot] = abs( temp - a[tot] ); } bool operator < (const node t) const { rep(i,1,tot) if ( a[i] != t.a[i] ) return a[i]<t.a[i]; return false; }};bool check(node t,int n){ rep(i,1,n) if ( t.a[i]!=0 ) return false; return true;}int n;map<node,int> flag;node x;int main(){ int T; cin>>T; while(T--) { cin>>n; flag.clear(); x.tot = n; rep(i,1,n) cin>>x.a[i]; rep(i,1,1000) { if ( flag[x] || check(x,n) ) break; flag[x] = 1; x.change(); } if ( check(x,n) ) puts("ZERO"); else puts("LOOP"); } return 0;}
0 0
- uva 1594
- uva 1594
- UVA 1594
- UVA 1594
- UVA 1594
- UVA 1594
- Uva 1594
- UVA 1594 - Ducci Sequence
- UVa 1594 Ducci Sequence
- UVa 1594 - Ducci Sequence
- UVa 1594 - Ducci Sequence
- UVA 1594 - Ducci Sequence
- Uva 1594 Ducci Sequence
- UVa 1594 Ducci Sequence
- Uva - 1594 - Ducci Sequence
- UVa 1594 Ducci队列
- UVa 1594 Ducci Sequence
- UVA 1594 Ducci Sequence
- MySQLi基于面向过程的编程
- UVA 1593
- poj2481Cows【树状数组】
- CodeForces 294A Shaass and Oskols
- MySQLi基于面向对象的编程
- UVA 1594
- MySQLi的高级应用
- no system images installed for target 问题的解决办法
- 函数指针 虚函数遐思
- hdoj5489Removed Interval【lis】
- UVA 10935
- neuq oj 1012 SZ斐波拉契数列 C++
- JVM类的加载过程
- UVA 10763