2020年 华东师大研究生机试题
A. 钥匙
代码:
#include <iostream>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int key[n];
for(int i=0;i<n;i++)
cin>>key[i];
int pos=0,door=0,sum=0,t=0;
while(k!=0){
if(key[pos]!=door+1){
pos=(pos+1)%n;
t++;
}
else{//key[pos]==door+1
door=(door+1)%n;
sum=sum+t;
k--;
t=0;
}
}
cout<<sum<<endl;
}
B. 上古计算机
代码:
#include <iostream>
#include <map>
using namespace std;
map<string,int> m;//用索引方式存储寄存器对应值
void mapinit(){
m["AX"]=0;
m["BX"]=0;
m["CX"]=0;
}
int main(){
mapinit();
string str,commd,reg1,reg2;
int num;
while(getline(cin,str)){
commd=str.substr(0,str.find(' '));
reg1=str.substr(str.find(' ')+1,2);
if(str.find(',')!=string::npos)//也可以写作str.find(',')!=-1 或 str.find(',')!=str.npos
reg2=str.substr(str.find(',')+1);
if(commd=="IN"){
num= stoi(reg2);
m[reg1]=num;
}
else if(commd=="MOV"){
m[reg1]=m[reg2];
}
else if(commd=="ADD"){
m[reg1]=m[reg1]+m[reg2];
}
else if(commd=="SUB"){
m[reg1]=m[reg1]-m[reg2];
}
else if(commd=="MUL"){
m[reg1]=m[reg1]*m[reg2];
}
else if(commd=="DIV"){
m[reg1]=m[reg1]/m[reg2];
}
else if(commd=="OUT"){
cout<<m[reg1]<<endl;
}
}
}
注意:可以直接使用c++自带函数实现类型转换
stoi() 和 atoi()
这两个功能虽然都是将字符串转化为 int 类型,但是还是有区别的,
stoi 的参数是 const string* 类型
atoi 的参数是 const char* 类型
stoi() 会对转化后的数进行检查,判断是否会超出 int 范围,如果超出范围就会报错;
atoi() 不会对转化后的数进行检查,超出上界,输出上界,超出下界,输出下界;
还有一点,如果使用 atoi 对字符串 string 进行转化的话,就需要 c_str() 函数将 const string* 类型 转化为 cons char* 类型
to_string()
功能:将数字常量(int,double,long等)转换为字符串(string),返回转换好的字符串
这个就没有那么多注意的地方了,记住功能和使用方法就可以了;
用法测试:
#include <iostream>
using namespace std;
int main(){
string str="12345";
cout<<stoi(str)<<endl;
cout<<atoi(str.c_str())<<endl;
int num=12345678;
cout<<to_string(num)<<endl;
}
C. 安全驾驶
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
int total;
cin>>total;
int n;
cin>>n;
int distance[n],speed[n];
double time[n];
for(int i=0;i<n;i++){
cin>>distance[i]>>speed[i];
time[i]=(total-distance[i])/(speed[i]*1.0);
}
sort(time,time+n);
printf("%.6lf\n",total/time[n-1]);
}
D. 表面积
代码:
#include <iostream>
#include <algorithm>
using namespace std;
struct obj{
long long r;
long long h;
};
bool compare1(obj x,obj y){
return x.r>y.r;
}
bool compare2(obj x,obj y){
return x.r*x.h>y.r*y.h;
}
int main(){
int n,m;
cin>>n>>m;
obj o[n];
long long res[n];
for(int i=0;i<n;i++){
long long r0,h0;
cin>>r0>>h0;
o[i].r=r0,o[i].h=h0;
res[i]=0;
}
sort(o,o+n, compare1);
for(int i=0;i<=n-m;i++){
res[i]+=o[i].r*o[i].r+2*o[i].r*o[i].h;
sort(o+i+1,o+n, compare2);
for(int j=0;j<m-1;j++)
res[i]+=o[i+j+1].r*o[i+j+1].h*2;
sort(o,o+n, compare1);
}
sort(res,res+n);
cout<<res[n-1]<<endl;
}
注意:这里所有数据都要用long long型,不然没法AC。
E. 达到回文数
代码:
#include <iostream>
using namespace std;
int Reverse(int x){
int y=0;
while (x){
y=y*10+x%10;
x=x/10;
}
return y;
}
int IsHuiwen(int x){
if(x== Reverse(x)) return 1;
else return 0;
}
int main(){
int n;
cin>>n;
int count=0;
while(IsHuiwen(n)==0){
n=n+ Reverse(n);
count++;
}
cout<<count<<" "<<n<<endl;
}
声明
文章部分内容来自:https://blog.csdn.net/qq_40394960/article/details/109131894
文章部分习题来自:https://acm.ecnu.edu.cn/

