一、1120 Friend Numbers (20 分)
分析:输入n个数,输出各个位数上数字和的总数。使用set可以轻松解决。
#include<bits/stdc++.h>
using namespace std;
set<int> s;
int getSum(int k){
int sum = 0;
while(k != 0){
sum += k % 10;
k = k / 10;
}
return sum;
}
int main(){
int n, k;
cin >> n;
for(int i=0; i<n; i++){
cin >> k;
s.insert(getSum(k));
}
cout << s.size() << endl;
for(auto it=s.begin(); it!=s.end(); it++){
if(it != s.begin()) cout << " ";
cout << *it;
}
return 0;
}
二、1121 Damn Single (25 分)
分析:输入n对情侣,再输出m个数,输出在这之中不是情侣的人(要成对的)。使用map来保存情侣,当一名情侣队列中,查询他的配偶是否在其中,一开始我套了两个for,然后发现超时了,之后用了find就可以了。有一个测试点是要输出左0的。
#include<bits/stdc++.h>
using namespace std;
map<int, int> ma;
vector<int> num, res;
int main(){
int n, m, a1, a2, k;
cin >> n;
for(int i=0; i<n; i++){
cin >> a1 >> a2;
ma[a1] = a2;
ma[a2] = a1;
}
cin >> m;
for(int i=0; i<m; i++){
cin >> k;
num.push_back(k);
}
for(int i=0; i<m; i++){
int flag = 0, temp = num[i];
if(ma.find(temp) != ma.end()){
if(find(num.begin(), num.end(), ma[temp]) != num.end()){
flag = 1;
}
}
if(flag == 0){
res.push_back(num[i]);
}
}
sort(res.begin(), res.end());
cout << res.size() << endl;
for(int i=0; i<res.size(); i++){
if(i != 0) cout << " ";
printf("%05d", res[i]);
}
return 0;
}
三、1122 Hamiltonian Cycle (25 分)
分析:输入一个图,包含路径。输入m行,判断每行的节点能否按顺序构成哈密顿回路。图论的哈密顿回路,回路的节点等于图的节点数加一,回路的首节点和末节点相等,回路要经过各个节点,并且经过路径一定要存在。
#include<bits/stdc++.h>
using namespace std;
int point[220], e[220][220];
int main(){
int n, m, a, b, k, s, v1;
cin >> n >> m;
for(int i=0; i<m; i++){
cin >> a >> b;
e[a][b] = 1;
e[b][a] = 1;
}
cin >> k;
for(int i=0; i<k; i++){
cin >> s;
vector<int> v;
bool visit[n+5] = {false};
for(int j=0; j<s; j++){
cin >> v1;
v.push_back(v1);
visit[v1] = true;
}
if(s != n+1 || v[0] != v[v.size()-1]){
cout << "NO" << endl;
}else{
int flag = 0;
for(int j=0; j<v.size()-1; j++){
if(e[v[j]][v[j+1]] != 1){
flag = 1;
}
if(visit[j+1] == false){
flag = 1;
}
}
if(flag == 0){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
}
}
return 0;
}
四、1123 Is It a Complete AVL Tree (30 分)
分析:输入n个数,将其构建成为一颗平衡二叉树,并输出层序遍历,并且判断是不是完全二叉树。因为我之前只是知道平衡二叉树的知识,完全不会具体怎么做,所以为了做这道题,特地去背了一套创建平衡二叉树的模板😄,层序遍历用深搜,判断完全二叉树也用深搜(所有节点保存到队列,遇到NULL退出循环,如果队列里剩下的只有NULL, 则为平衡二叉树,因为平衡二叉树的NULL只能在末节点),考试前还要多默写几遍AVL的构建😓。
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
vector<int> res;
int getHeight(struct Node* tree){
if(tree == NULL){
return 0;
}
int l = getHeight(tree->left);
int r = getHeight(tree->right);
return max(l, r) + 1;
}
struct Node* RightRotate(struct Node* tree){
struct Node* temp = tree->right;
tree->right = temp->left;
temp->left = tree;
return temp;
}
struct Node* LeftRotate(struct Node* tree){
struct Node* temp = tree->left;
tree->left = temp->right;
temp->right = tree;
return temp;
}
struct Node* RightLeftRotate(struct Node* tree){
tree->right = LeftRotate(tree->right);
return RightRotate(tree);
}
struct Node* LeftRightRotate(struct Node* tree){
tree->left = RightRotate(tree->left);
return LeftRotate(tree);
}
struct Node* Insert(struct Node* tree, int val){
if(tree == NULL){
tree = new Node();
tree->data = val;
tree->left = tree->right = NULL;
return tree;
}
if(val < tree->data){
tree->left = Insert(tree->left, val);
int l = getHeight(tree->left);
int r = getHeight(tree->right);
if(l - r >= 2){
if(val < tree->left->data){
tree = LeftRotate(tree);
}else{
tree = LeftRightRotate(tree);
}
}
}
else{
tree->right = Insert(tree->right, val);
int l = getHeight(tree->left);
int r = getHeight(tree->right);
if(r - l >= 2){
if(val > tree->right->data){
tree = RightRotate(tree);
}else{
tree = RightLeftRotate(tree);
}
}
}
return tree;
}
void bfs(struct Node* tree){
queue<Node*> q;
struct Node* root = tree;
q.push(root);
while(!q.empty()){
res.push_back(root->data);
if(root->left != NULL){
q.push(root->left);
}
if(root->right != NULL){
q.push(root->right);
}
q.pop();
root = q.front();
}
}
void bfs2(struct Node* tree){
queue<Node*> q;
struct Node* root = tree;
q.push(root);
while(!q.empty()){
if(root == NULL) break;
q.push(root->left);
q.push(root->right);
q.pop();
root = q.front();
}
root = q.front();
while(!q.empty()){
if(root != NULL){
cout << "NO" << endl;
return;
}
q.pop();
root = q.front();
}
cout << "YES" << endl;
}
int main(){
int n, k;
cin >> n;
Node* tree = NULL;
for(int i=0; i<n; i++){
cin >> k;
tree = Insert(tree, k);
}
bfs(tree);
for(int i=0; i<res.size(); i++){
if(i != 0) cout << " ";
cout << res[i];
}
cout << endl;
bfs2(tree);
return 0;
}