C++: UVa exercise 1594 Ducci Sequence


#include <iostream>
#include <vector>
#include <map>

using namespace std;
const int maxn=1005;
vector<int> a[maxn];
map<vector<int>, int> cnt;


int main(int argc, const char * argv[]) {
    int n, i,j=0, temp,flag=-1, T;
    scanf("%d", &T);
    while(T--){
        cnt.clear();
        cin >> n;
        j = 0;
        a[j].clear();
        for(i=0;i<n;i++){
            a[j].push_back(0);
        }
        cnt[a[j]]=2;
        j++; // j =1
        a[j].clear();
        for(i=0;i<n;i++){
            cin >> temp;
            a[j].push_back(temp);
        }
        while(1){
            j++;
            a[j].clear();
            for(i=0;i<n;i++){
                if(i< n-1) a[j].push_back(abs(a[j-1][i] -a[j-1][i+1]));
                else a[j].push_back(abs(a[j-1][i] -a[j-1][0]));
            }
            if(!cnt[a[j-1]]) cnt[a[j-1]]=0;
            cnt[a[j-1]]++;

            if(cnt[a[j-1]]==2){
                flag =1; //LOOP
                break;
            }
            else if(cnt[a[j-1]]==3){
                flag=0; //Zero
                break;
            }
            }
        if(flag==0){
            cout << "ZERO"<<'n';
        }
        else if(flag==1){
            cout << "LOOP" << 'n';
        }
        }
    cout << "Finished" << 'n';
    return 0;
}

I am trying to deal with this problem by this chunk of code. But the code just isn’t right here. I can’t find why. I use the test data from here[https://www.udebug.com/UVa/1594] and it always stops at around 40 sets of data and just stops there without throwing any error. Hope someone can help me out here.



لینک منبع

پاسخ دهید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *