2022-08-20-荣耀笔试3题
不好意思,没注意时间,重新发一次
和下午的网易没法比,不需要监控手机
很简单,但第二题斐波拉契做了好久,最后python还是不知道错在哪里
第一题有点麻烦,需要解析字符串
// 第1题
// 18min 100% 100'
#include <iostream>
#include<vector>
using namespace std;
int main(){
string s;cin>>s;
string ans;
int i=2,r=0,n,m; // the only bug: r 没初始化。。。
vector<int> a,b;
while(s[i]!='}'){
int c=0;
i++;
while(s[i]!=','&&s[i]!='}'){
c=c*10+s[i++]-'0';
}
a.push_back(c);
}
n=a.size();
i+=4;
while(s[i]!='}'){
int c=0;
i++;
while(s[i]!=','&&s[i]!='}'){
c=c*10+s[i++]-'0';
}
b.push_back(c);
}
m=b.size();
i+=4;
while(i<s.length())
r=r*10+s[i++]-'0';
int j=0;
// cout<<"r= "<<r<<"\n";
for(int i=0;i<n;i++){
while(j<m&&a[i]>b[j])
j++;
if(j==m)
continue;
else if(b[j]-a[i]>r){
ans+="("+to_string(a[i])+","+to_string(b[j])+")";
continue;
}
int j2=j;
while(j2<m&&b[j2]-a[i]<=r){
// cout<<"bj= "<<b[j2]<<"\n";
ans.append("(").append(to_string(a[i])).append(",").append(to_string(b[j2])).append(")");
j2++;
}
}
cout<<ans;
return 0;
}
// 第2题
// python75%
from functools import cached_property
n=int(input(''))
m=int(input(''))
c = [0]*(n+1)
c[1]=1
c[2]=2
c[3]=3
# @cached_property()
def f(i):
if i<=3:
return i
elif c[i]!=0:
return c[i]
c[i]=(f(i-1)+f(i-2))%m
return c[i]
f(n)
sum_=0
for i in c:
sum_ = (sum_+i)%m
print(sum_)
//C++ 16min 100% 200'
#include <iostream>
#include <string.h>
using namespace std;
int main(){
int n,m;cin>>n>>m;
int f[n+1];
// memset(f,0,sizeof(int)*(n+1));
f[0]=0; // bug2: 没初始化
f[1]=1;
f[2]=2%m;
for(int i=3;i<=n;i++){
f[i]=(f[i-1]+f[i-2])%m;
}
int a=0; // bug1: 没看清题意要求和,直接返回f[n]了
for(auto i:f) // 空间可以降到 O(1)
a=(a+i)%m;
cout<<a;
return 0;
}
// 4min 100% 300'
#include<iostream>
#include<string>
#include<map>
int main(){
int n;std::cin>>n;
std::string s;
std::map<std::string, int> c;
while(n--){
std::cin>>s;
c[s]++;
}
for(auto& [i,cnt]:c)
printf("%s %d\n",i.c_str(),cnt);
return 0;
} #荣耀笔试##笔试##23秋招#

