2025年度哈尔滨理工大学软件工程大一上期末考试题解
1.https://ac.nowcoder.com/acm/contest/100070/F
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
bool highest(vector<vector<int> > &arr,int x,int y,int n,int m){\\用vector声明二维数组时,可以在自己声明的函数里面直接调用数组在新的函数里面使用,如vector<vector<int> &数组名来调用
if(x>0&&arr[x-1][y]>=arr[x][y]\\判断是否是山峰,需要判断是否在上下左右都是最高的。但是又会出现诸如在第一排第一个,从而导致其左边和上边没有东西,进而不需要比较。
||x<n-1&&arr[x+1][y]>=arr[x][y]
||y>0&&arr[x][y-1]>=arr[x][y]
||y<m-1&&arr[x][y+1]>=arr[x][y]
)return false;
return true;
}
void swap(int &a,int &b){//&代表直接引用
int zhong=a;
a=b;
b=zhong;
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int> > arr(n,vector<int>(m));
for(int i=0;i<n;i++){
for(int g=0;g<m;g++){
cin>>arr[i][g];
}
}
int T;
cin>>T;
int sum=0;
for(int i=0;i<T;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;//输入的都是从第一行开始的,所以要-1,因为计算机的数组都是从0开始的
--x1;
--y1;
--x2;
--y2;
swap(arr[x1][y1],arr[x2][y2]);
}
for(int i=0;i<n;i++){
for(int g=0;g<m;g++){
if(highest(arr,i,g,n,m))sum++;
}
}
cout<<sum<<endl;
for(int i=0;i<n;i++){
for(int g=0;g<m;g++){
if(highest(arr,i,g,n,m))cout<<i+1<<" "<<g+1<<endl;
}
}
return 0;
}
2.https://ac.nowcoder.com/acm/contest/100070/G
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
using namespace std;
int main(){
string a,b;
int n;
getline(cin,a);
cin>>n;
for(int i=0;i<a.length();i++){
if(a[i]>='a'&&a[i]<='z'){
char c='a'+(a[i]-'a'+n%26+26)%26;
b+=c;
}
else if(a[i]>='A'&&a[i]<='Z'){
char c='A'+(a[i]-'A'+n%26+26)%26;
b+=c;
}
else b+=a[i];
}
cout<<b;
return 0;
}
凯撒密码的原理
用大写字母为例,我们优先把大写字母从‘A’到‘Z'依次赋成0-25,然后我们假设字母的偏移量为k(k可以为正,也可以是负的),假设输入的字母为k1,故而首先由k1-'A',得到字母的简化值,然后加上k,得到k1-‘A'+k,再然后+26,因为k可能是负的,如果k非常小,假如为-28的时候,此时得到的字母的对应值为负,但是我们声明的对应值就只有0-25,故而加上26,他不会影响我们的结果,因为我们会对他们的结果%26,故而没有影响,得到(k1-‘A’+k+26)%26,得到我们预先设定的对应值,然后再加上‘A’后,可以得到了对应的字母;
3.https://ac.nowcoder.com/acm/contest/100070/I
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct book{
string name;
int year;
double sell;
};
bool compare(book &a,book &b){
return a.sell>b.sell;
}
int main(){
int n;
cin>>n;//cin输入后,就会留下一个换行符在系统里面
vector<book> books(n);
cin.ignore();//消除由cin引发的换行符
for(int i=0;i<n;i++){
getline(cin,books[i].name);//getline是一个很特殊的输入方式,遇到了换行符\n的时候就会结束
cin>>books[i].year;
cin>>books[i].sell;
cin.ignore();
}
sort(books.begin(),books.end(),compare);
printf("%.2lf, %s, %d\n",books[0].sell,books[0].name.data(),books[0].year);
printf("%.2lf, %s, %d",books[n-1].sell,books[n-1].name.c_str(),books[n-1].year);
return 0;
}
4.https://ac.nowcoder.com/acm/contest/100070/J
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Actor{
public:
virtual void show()const=0;//如果虚构函数中用了const,那么接下来,在所有的子类里面,用这个函数的时候,都要在括号后面写const。如果不写const,后面就不用写。
};
class God:public Actor{
private:
string Name;
string Place;
int Power;
public:
void show()const{
cout<<Place<<","<<Name;
}
God(string a,string b,int c):Name(a),Place(b),Power(c){}
int getpower(){
return Power;
}
};
class Monster:public Actor{
private:
string Name;
string Prototype;
int Power;
public:
void set(string a,string b,int c){
Name=a;
Prototype=b;
Power=c;
}
void show()const{
cout<<Name<<" whose prototype was "<<Prototype;
}
int getpower(){
return Power;
}
};
int operator-(God &g,Monster &m){
//参数列表里的顺序,就是重载运算符里面的运算顺序,如果参数列表不变,把return后面改成m.getpower()-g.getpower(),就会报错,如果重载运算符想要同时满足g-m和m-g,就要重新写一个,如
//
int operator-(Monster &m,God &g)来写
return g.getpower()-m.getpower();
}
int main(){
string n,p;
int po,s;
cin>>n>>p>>po;
God g(n,p,po);
cin>>n>>p>>po;
Monster m;
m.set(n,p,po);
s=g-m;
if(s>0){
g.show();
cout<<" > ";
m.show();
}
else if(s==0) {
g.show();
cout<<" = ";
m.show();
}
else {
g.show();
cout<<" < ";
m.show();
}
cout<<endl;
return 0;
}

