250104-C10-结构体
原题链接:33265.笔记 汇总2025-01-04 18:45:47
发布于:江苏
一、练习回顾
回顾练习:A29915.小码君找鞍点
利用函数进行每个点的判断,函数中实现鞍点的判断功能。
#include <iostream>
using namespace std;
int a[10][10];
bool is_andian(int x, int y){
int row_max = a[x][1];
int col_min = a[1][y];
for (int i=1; i<=5; i++){
row_max = max(row_max, a[x][i]);
col_min = min(col_min, a[i][y]);
}
if (row_max == col_min) return 1;
return 0;
}
int main() {
for (int i=1; i<=5; i++){
for (int j=1; j<=5; j++){
cin >> a[i][j];
}
}
for (int i=1; i<=5; i++){
for (int j=1; j<=5; j++){
if(is_andian(i, j)){
printf("%d %d %d", i, j, a[i][j]);
return 0;
}
}
}
cout<<"not found";
return 0;
}
二、数组排序
1. bubble sort
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
int a[105], n=10000;
void print(){
for (int i=1; i<=n; i++)
cout<<a[i]<<' ';
cout << endl;
}
//冒泡排序
void bubble_sort(){
//n个数需要比较n-1趟
for (int i=1; i<=n-1; i++){
//n-i次
for (int j=1; j<=n-i; j++){
if (a[j] > a[j+1]) {
swap (a[j], a[j+1]);
}
}
}
}
int main() {
n = 20;
srand(time(0)); //设置随机种子数字
for (int i=1; i<=n; i++){
a[i] = rand()%50+1;
}
print();
bubble_sort();
print();
return 0;
}
(1). 随机数反推
结论: rand()%(b-a+1) ==> [a, b]
rand()%n---> [0, n-1]
1-50
rand()%50 ---> [0, 49]
逆向推导: 从结果出发反推
[30, 50]
[0, ?] + 30---> [30, 50]---> ? = 50-30 = 20
rand()%(b-a+1) ==> [a, b]
(2). 冒泡排序的算法过程演示:可视化算法超链接
核心是两两[相邻]比较
23 12 78 2 9
12 23 2 9 78 第一趟结束 比较4次
12 2 9 23 78 第二趟结束 比较3次
2 9 12 23 78 第三趟结束 比较2次
2 9 12 23 78 第四趟结束 比较1次
第i趟需要比较n-i次
n = 5
最多需要n-1趟
2. sort神器
需要头文件 #include <algorithm>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
int a[105], n=10000;
void print(){
for (int i=1; i<=n; i++)
cout<<a[i]<<' ';
cout << endl;
}
//自定义比较规则
bool cmp(int x, int y){
return x > y;
}
int main() {
n = 20;
srand(time(0)); //设置随机种子数字
for (int i=1; i<=n; i++){
a[i] = rand()%50+1;
}
print();
sort(a+1, a+1+n, cmp);
/*
(默认从小到大排序)
参数1: 开始排序的地址 (&, 指针)
参数2: 结束排序的地址
参数3: cmp, 自定义的比较规则, (可以省略)
*/
print();
return 0;
}
三、结构体
1.结构体的定义
#include <iostream>
using namespace std;
//结构体类型
struct student{
string name;
int id;
double score;
} b, c, d;
//定义了一个结构体类型,类型的名字叫 student
//注意这里的b,c,d是变量名称
int main() {
// int a;
// student b;
//赋值方式1:
b.name = "xiaoming";
b.score = 99.5;
cout << b.name << ' ' << b.id << endl;
//赋值方式2:
c = {"张飞", 1003, 90};
cout<< c.name << ' '<< c.id << ' '<<c.score << endl;
return 0;
}
2. 结构体数组
- A7863.【结构体】结构体数组的遍历
#include <iostream>
using namespace std;
struct node{
// char name[20];
string name;
int age;
double score;
}a[1005]; //结构体数组
int main() {
int n;
cin >> n;
for (int i=1; i<=n; i++){
cin >>a[i].name >> a[i].score >> a[i].age;
}
for (int i=1; i<=n; i++){
printf("%s %.1lf %d\n", a[i].name.c_str(), a[i].score, a[i].age);
}
return 0;
}
注意占位符 %s需要进行一下格式的转换c_str()
3. 结构体成员函数的使用
- A30638.【结构体】【入门】采蘑菇
#include <iostream>
using namespace std;
struct node{
string name; //成员变量
int x, y, z;
void in(){ //成员函数
cin>>name>>x>>y>>z;
}
void out(){
cout<<name<<' '<<x<<' '<<y<<' '<<z<<endl;
}
}a[105];
int main() {
int n;
cin >> n;
for (int i=1; i<=n; i++) a[i].in(); //调用成员函数
for (int i=n; i>=1; i--) a[i].out();
return 0;
}
四、结构体排序
实例1: A29740.【结构体排序】最厉害的学生
#include <bits/stdc++.h>
using namespace std;
struct node{
string name;
int x, y, z, sum;
}a[1005];
//结构体自定义比较规则
bool cmp(node g, node h){
return g.sum > h.sum;
}
int main() {
int n;
cin >> n;
for(int i=0; i<n; i++){
cin>>a[i].name>>a[i].x>>a[i].y>>a[i].z;
a[i].sum = a[i].x+a[i].y+a[i].z;
}
sort(a, a+n, cmp); //结构体排序中必须要写cmp
cout<< a[0].name<<' '<< a[0].x<<' '<<a[0].y<<' '<<a[0].z;
return 0;
}
实例2: A30641.【结构体排序】生日
#include <bits/stdc++.h>
using namespace std;
struct node{
string name;
int y, m, d, idx;
}a[10005];
bool cmp(node r, node c){
if (r.y != c.y) return r.y < c.y; //年份不同比较年份
else if (r.m != c.m) return r.m < c.m; //月份不同比较月份
else if (r.d != c.d) return r.d < c.d; //天数不同比较天数
else return r.idx > c.idx; //全部相同比较序号
}
int main() {
int n;
cin >> n;
for(int i=1; i<=n; i++){
cin>>a[i].name>>a[i].y>>a[i].m>>a[i].d;
a[i].idx = i; //序号记录登记的顺序
}
sort (a+1, a+1+n, cmp);
for(int i=1; i<=n; i++){
cout << a[i].name << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个