当前位置: 代码迷 >> 综合 >> 习题5-2 UVA1594 Ducci序列(vector,set) 习题5-3 UVA 10935 卡片游戏(queue)
  详细解决方案

习题5-2 UVA1594 Ducci序列(vector,set) 习题5-3 UVA 10935 卡片游戏(queue)

热度:68   发布时间:2023-12-06 19:03:10.0

习题5-2

//使用vector和set
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
int T,n;
int ele;
int zero( vector<int> V,int num)
{for(int i=0;i<int(V.size());i++){if(V[i]!=0){return 0;}}return 1;
}
int main()
{cin>>T;while(T--){cin>>n;set<vector<int> >sequence;vector<int>a[1005];for(int i=0;i<n;i++){cin>>ele;a[0].push_back(ele);}sequence.insert(a[0]);if(zero(a[0],n)){cout<<"ZERO\n";continue;}for(int i=1;i<=1001;i++){for(int k=0;k<n-1;k++){a[i].push_back(abs(a[i-1][k]-a[i-1][k+1]));}a[i].push_back(abs(a[i-1][n-1]-a[i-1][0]));if(sequence.count(a[i])){cout<<"LOOP\n";break;}if(zero(a[i],n)){cout<<"ZERO\n";break;}sequence.insert(a[i]);}}return 0;
}

习题5-3

//使用队列
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n;
int main()
{while((cin>>n)&&n){queue<int>q;for(int i=1;i<=n;i++){q.push(i);}cout<<"Discarded cards:";if(n>=2){cout<<" ";}while(q.size()!=1){if(q.size()>2){cout<<q.front()<<", ";q.pop();}else{cout<<q.front();q.pop();}int a=q.front();q.pop();q.push(a);}cout<<endl;cout<<"Remaining card: "<<q.front()<<endl;}return 0;
}

 

 

  相关解决方案