UVa1594-Ducci序列

问题不在赘述,放上原文链接,https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4469

注意:在前两个数作差求绝对值以后,数组第一个数被改写,因此在每次作差操作前,要保存第一个数,防止出错

#include <iostream>
#include <vector>
#include <map>
#include <math.h>
using namespace std;

typedef vector<int> V;
map<V,int> ID;
V v;

bool find_id(V &v){
	if( !ID.count(v) ){
		ID[v] = (int)(ID.size() - 1);
		return false;
	}
	return true;
}

int main(){
	int x;
	int n;
	cin >> n;
	while(n--){
		int t;
		cin >> t;
		v.clear();        // 每一次数据都要清空vector 和 map 
		ID.clear();
		while(t--){
			cin >> x;
			v.push_back(x);
		}
		int len = (int)v.size();
		v.push_back(v[0]);       // 因为最后一个数要与第一个数作差,这里增加一个空间存放第一个数,同时避免j+1越界。 
		while(true){
			int sum = 0;
			v[len] = v[0];
			for(int j = 0; j < len ; j++){
				sum += v[j];
				v[j] = abs(v[j] - v[j+1]);	
			}
			if(sum == 0){          //  sum得0,表示每个数都是0,退出循环 
				cout << "ZERO"<< endl;
				break;
			}
			if(find_id(v)){         //   ID为已存在的,是循环。 
				cout << "LOOP" << endl;
				break;
			}
		}	
	}	
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值