说明:
博主刚学习C++不久,为了提早学习数据结构和C++的一些知识,自己琢磨外加查阅资料写了这些题的代码,题目来源于22年初的学院老师组织的算法基础公选课的练习。我的代码甚至思路肯定存在许多不足和错误,欢迎大家批评指正。
题目列表:
问题 A: 按日期排序
思路:学会使用vector的push_back()来读未知数量的数据,vector里的数据类型可以是自己定义的一个结构体date,包含month,day,year,然后使用sort()函数进行排序,输出排序后的结果
注意:未定义大小的vector不能使用下标赋值,但是要输出有数据的未定义大小的vector,可以使用下标进行索引
参考代码:
#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//const int N = 1e4+5;
struct date{
int month,day,year;
};
vector<date> v;
bool cmp(date d1,date d2){//按照日期排序,时间早的排在前面,晚的排在后面
if(d1.year!=d2.year) return d1.year<d2.year;
else if(d1.year==d2.year&&d1.month!=d2.month) return d1.month<d2.month;
else return d1.day<d2.day;
}
int main(){
//问题 A: 按日期排序
// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
date temp;
//读取一个日期,并且放在vector的最后
while(~scanf("%d/%d/%d",&temp.month,&temp.day,&temp.year)) v.push_back(temp);
sort(v.begin(),v.end(),cmp);
//根据题意,使用%02d和%04d进行格式化输出
for(int i = 0;i<v.size();i++) printf("%02d/%02d/%04d\n",v[i].month,v[i].day,v[i].year);
return 0;
}
问题 B: 计算矩形面积
思路:使用一个结构体存放两个点的坐标,然后开一个数据类型为该结构体的vector,读入两个坐标,通过push_back()放进数组,后续通过sort()进行排序,排序的时候需要计算矩形的面积,最后输出排序后的面积即可
参考代码:
#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//const int N = 1e4+5;
//using PII = pair<int,int>;
struct square{
int x1,y1,x2,y2;//两个点的坐标(x1,y1),(x2,y2)
};
vector<square> v;
bool cmp(square s1,square s2){
//计算两个矩形的面积大小,并按照面积从小到大排序
return (abs(s1.x2-s1.x1)*abs(s1.y2-s1.y1))<(abs(s2.x2-s2.x1)*abs(s2.y2-s2.y1));
}
int main(){
//问题 B: 计算矩形面积
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
square temp;
//读入每个矩形的两个顶点
while(cin >> temp.x1 >> temp.y1 >> temp.x2 >> temp.y2) v.push_back(temp);
sort(v.begin(),v.end(),cmp);
for(int i = 0;i<v.size();i++) cout << abs(v[i].x2-v[i].x1)*abs(v[i].y2-v[i].y1) << '\n';
return 0;
}
问题 C: 火星数排序
思路:同理,定义一个同时包含火星数和地球数的结构体,然后开一个为该结构体数据类型的vector,依次读入火星数后,转换为地球数,再使用sort()通过地球数大小进行排序,输出排序后的火星数即可
注意:这个题每组数据都有给定大小n,所以我们可以固定vector大小来做,但是要猜出它最大的数据量级别,我这里第一次使用了1e3就通过了。同理,不固定vector长度也能做。
参考代码1:(使用定长vector来做)
#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 1e3+5;
struct number{
int earth,Mars;
};
vector<number> v(N);//相当于number v[N];
int transformation(int Mars){//火星数转换为地球数
int earth = 0;
int index = 0;
while(Mars!=0){
if(Mars%10==0) earth += 0*pow(10,index);
else if(Mars%10==8) earth += 1*pow(10,index);
else if(Mars%10==1) earth += 2*pow(10,index);
else if(Mars%10==5) earth += 3*pow(10,index);
else if(Mars%10==2) earth += 4*pow(10,index);
else if(Mars%10==3) earth += 5*pow(10,index);
else if(Mars%10==9) earth += 6*pow(10,index);
else if(Mars%10==4) earth += 7*pow(10,index);
else if(Mars%10==7) earth += 8*pow(10,index);
else if(Mars%10==6) earth += 9*pow(10,index);
index++;
Mars /= 10;
}
return earth;
}
bool cmp(number n1,number n2){//通过地球数大小升序排序
return n1.earth<n2.earth;
}
int main(){
//问题 C: 火星数排序
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int Case;cin >> Case;
while(Case--){
int n;cin >> n;
for(int i = 1;i<=n;i++) cin >> v[i].Mars;
//计算对应的地球数
for(int i = 1;i<=n;i++) v[i].earth = transformation(v[i].Mars);
sort(v.begin()+1,v.begin()+1+n,cmp);
for(int i = 1;i<=n;i++) cout << v[i].Mars << ' ';
cout << '\n';
}
return 0;
}
参考代码2:(使用不定长的vector来做)
#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 1e3+5;
struct number{
int earth,Mars;
};
vector<number> v;
int transformation(int Mars){//火星数转换为地球数
int earth = 0;
int index = 0;
while(Mars!=0){
if(Mars%10==0) earth += 0*pow(10,index);
else if(Mars%10==8) earth += 1*pow(10,index);
else if(Mars%10==1) earth += 2*pow(10,index);
else if(Mars%10==5) earth += 3*pow(10,index);
else if(Mars%10==2) earth += 4*pow(10,index);
else if(Mars%10==3) earth += 5*pow(10,index);
else if(Mars%10==9) earth += 6*pow(10,index);
else if(Mars%10==4) earth += 7*pow(10,index);
else if(Mars%10==7) earth += 8*pow(10,index);
else if(Mars%10==6) earth += 9*pow(10,index);
index++;
Mars /= 10;
}
return earth;
}
bool cmp(number n1,number n2){//通过地球数大小升序排序
return n1.earth<n2.earth;
}
int main(){
//问题 C: 火星数排序
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int Case;cin >> Case;
while(Case--){
int n;cin >> n;
v.clear();//清空数组
int tempM,tempe;//暂存火星数和地球数,后续都会被放进vector
for(int i = 0;i<n;i++){
cin >> tempM;
//计算tempM对应的地球数
tempe = transformation(tempM);
v.push_back({tempe,tempM});//推进vector
}
sort(v.begin(),v.end(),cmp);
for(int i = 0;i<v.size();i++) cout << v[i].Mars << ' ';
cout << '\n';
}
return 0;
}
问题 D: 文件名排序
思路:先观察出排序规则,先按照文件属性排序,文件夹排在文件前面,然后再根据文件名按字典序排序。申明一个包含文件名和文件属性的结构体,开一个该结构体数据类型的vector,读入后使用sort()排序,输出排序后的结果即可
参考题解:
#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
//const int N = 1e4+5;
struct file{
string name,format;//文件名,文件属性(文件格式)
};
vector<file> v;
bool cmp(file f1,file f2){
//先根据文件属性排序,再根据文件名排序
if(f1.format != f2.format) return f1.format<f2.format;
else return f1.name<f2.name;
}
int main(){
//问题 D: 文件名排序
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
file temp;
while(cin >> temp.name >> temp.format) v.push_back(temp);
sort(v.begin(),v.end(),cmp);
for(int i = 0;i<v.size();i++) cout << v[i].name << ' ' << v[i].format << '\n';
return 0;
}