今天來解YKL19(UVA11063):B2-Sequence*CPE48-23
判斷是否為B2-Sequence
1.檢查數列是否嚴格遞增
2.用Set儲存並檢查seq[i] + seq[j]的和是否重複
3.如果嚴格遞增且所有和均為唯一,則該數列為 B2-Sequence;否則不是
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main(){
int casenumber=1;
int n;
while(cin >> n){
vector<int> seq(n);
set<int> sums;
bool isb2s = true;
for(int i=0;i<n;i++){
cin >> seq[i];
}
for(int i=1;i<n;i++){
if(seq[i] <= seq[i-1]){
isb2s = false;
break;
}
}
if(isb2s){
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
int sum = seq[i] + seq[j];
if(sums.count(sum)){
isb2s = false;
break;
}
sums.insert(sum);
}
if(!isb2s)break;
}
}
if (isb2s) cout << "Case #" << casenumber << ": It is a B2-Sequence." << endl;
else cout << "Case #" << casenumber << ": It is not a B2-Sequence." << endl;
cout << endl;
casenumber++;
}
return 0;
}