22真题
作者:
Ksiiiii
,
2024-11-28 21:28:47
,
所有人可见
,
阅读 1
无人机打靶
//很常规的结构体排序,直接三板斧就行,自定义swap,bubblesort,然后输出就好了
#include <iostream>
#include <string>
using namespace std;
const int N = 101;
struct airplane{
string name;
int score;
}P[N];
void swap(airplane &a,airplane &b){
airplane t = a;
a = b;
b = t;
}
bool bubblesort(airplane P[],int n){
for(int i = 0;i < n;i ++){
bool flag = false;
for(int j = 0;j < n - i -1;j ++){
if(P[j].score < P[j+1].score){
swap(P[j],P[j+1]);
flag = true;
}
}
if(!flag) break;
}
}
int main(){
int n ;
cin >> n;
int blue = 0;
int red = 0;
int green = 0;
int purple = 0;
for(int i = 0;i < n;i ++){
cin >> P[i].name >> P[i].score;
if(P[i].name == "b") blue ++;
else if(P[i].name == "r") red ++;
else if(P[i].name == "g") green ++;
else if(P[i].name == "p") purple ++;
}
cout << blue <<endl << red <<endl<< green <<endl<< purple << endl;
bubblesort(P,n);
for(int j = 0;j < n;j ++){
cout << P[j].score << " "<< P[j].name <<endl;
}
return 0;
}
扑克牌中的顺子
#include <iostream>
#include <algorithm>
using namespace std;
// 判断五张牌是否构成顺子的函数
bool isStraight(int nums[5]) {
int appeared[14] = {0}; // 用于模拟记录牌面数值是否出现过,索引对应牌面数值(0 - 13),初始化为0表示都未出现
int maxVal = 0;
int minVal = 14;
for (int i = 0; i < 5; i++) {
int num = nums[i];
if (num == 0) {
continue;
}
// 如果对应牌面数值已经标记为出现过(即appeared[num]为1),说明有重复牌,返回false
if (appeared[num]) {
return false;
}
appeared[num] = 1; // 将对应牌面数值标记为已出现
maxVal = max(maxVal, num);
minVal = min(minVal, num);
}
return maxVal - minVal < 5;
}
int main() {
int inputCards[5];
for (int i = 0; i < 5; i++) {
cin >> inputCards[i];
}
if (isStraight(inputCards)) {
cout << "这五张牌是顺子" << endl;
} else {
cout << "这五张牌不是顺子" << endl;
}
return 0;
}
计算机视觉(递归实现指数型枚举)