寒武纪 2019.9.17笔试 3.2
5选择4编程
选择很水
编程题有会的请告诉我。。。
第一题,
给AB两个队和一个target,把数字往队里放,如果每个队对每个数,都有另一个数对应它门的和是target,那么这个队好。两个队对都好还是啥的,忘了。
按贪心随便写了些,懒得改交卷了全局情况0.3
#include<iostream> #include<string> #include<vector> #include<map> using namespace std; void res(int N,int X,int Y,vector<int>&input) { int flag = false; map<int,int>record; for(int i=0;i<N;++i) record[input[i]]=-1; //record.insert({input[i],-1}); //int index=0; for(int i=0;i<N;++i) { if(record[input[i]]!=-1) continue; if(record.find(X-input[i])!=record.end()) { if(record[X-input[i]]==-1) { record[X-input[i]]=X; record[input[i]]=X; } else if(record[X-input[i]]==Y) { //index=i; flag=true; break; } }else if(record.find(Y-input[i])!=record.end()) { if(record[Y-input[i]]==-1) { record[Y-input[i]]=Y; record[input[i]]=Y; } else if(record[Y-input[i]]==X) { flag=true; //index=i; break; } }else { flag = true; break; } } if(flag) { cout<<"NO"<<endl; //cout<<index<<endl; //cout<<record[index]<<endl; return ; } cout<<"YES"<<endl; for(int i=0;i<N-1;++i) { cout<<record[input[i]]<<" "; } cout<<record[input[N-1]]<<endl; return; } int main() { int N,X,Y; cin>>N>>X>>Y; vector<int>data(N,0); for(int i=0;i<N;++i) { cin>>data[i]; } res(N,X,Y,data); }第二题 给一些数,然后压缩成什么东西,可以选一个上下阈值,两边删掉,问最少删多少个
滑窗,可以改成二分0.9
#include<iostream> #include<string> #include<vector> #include<map> #include<algorithm> typedef long long LL; using namespace std; LL maxmax(LL &a,LL &b) { if(a<b)return b; return a; } void print_arr(vector<LL>&data) { for(auto c:data)cout<<c<<" "; cout<<endl; } LL res(vector<LL>input,LL N,LL M) { M*=8; LL bitNum=M/N; LL nums; if(bitNum==0) { //cout<<'a'<<endl; return N-1; } if(bitNum<31) nums = pow(2,bitNum); else { //cout<<'b'<<endl; return 0; } sort(input.begin(),input.end()); vector<LL>arrNum(1,0); vector<LL>arrGeshu(1,0); for(int i=0;i<N;) { int j=i; while(j<N && input[i]==input[j])++j; arrNum.push_back(input[i]); arrGeshu.push_back(j); i=j; } //print_arr(input); //print_arr(arrGeshu); //无符号比LL,害怕 if(arrNum.size()<nums) { //cout<<'c'<<endl; return 0; } LL t_max=0; for(unsigned int i=0;i<arrNum.size()-nums;++i) { //if(arrGeshu[i+nums]-arrGeshu[i]>0)cout<<'>'<<endl; if(t_max<arrGeshu[i+nums]-arrGeshu[i])t_max=arrGeshu[i+nums]-arrGeshu[i]; } //t_max = maxmax(t_max,arrGeshu[i+nums]-arrGeshu[i]); return N-t_max; } int main() { LL N,M; cin>>N>>M; vector<LL>data(N,0); for(int i =0;i<N;++i)cin>>data[i]; cout<<res(data,N,M)<<endl; }字符串匹配,B串在A串顺序出现 1.0
#include<iostream> #include<string> using namespace std; string res(string &s1,string &s2) { if(s2.size()>s1.size()) { return "NO"; } int cur = 0; int len = s2.size(); for(auto c:s1) { if(cur==len) return "SUB"; if(c==s2[cur]) ++cur; } if(cur==len) return "SUB"; return "NO"; } int main() { int n; cin>>n; while(n--) { string s1,s2; cin>>s1>>s2; cout<<res(s1,s2)<<endl; } }池化层手撸1.0
#include<iostream> #include<string> #include<vector> using namespace std; void print_res(vector<vector<int>>&data,int hang,int lie) { for(int i=0;i<hang;i++) { for(int j=0;j<lie-1;j++) { cout<<data[i][j]<<" "; } cout<<data[i][lie-1]<<endl; } } int main() { int input_h,input_l; cin>>input_h>>input_l; int chi_h,chi_l; cin>>chi_h>>chi_l; int stride_h,stride_l; cin>>stride_h>>stride_l; vector<vector<int>>data; for(int i=0;i<input_h;++i) { vector<int>tmp(input_l,0); for(int j=0;j<input_l;++j) { cin>>tmp[j]; } data.push_back(tmp); } int output_h = 1+(input_h-chi_h)/stride_h; int output_l = 1+(input_l-chi_l)/stride_l; vector<vector<int>>out(output_h,vector<int>(output_l,0)); for(int i=0;i<output_h;++i) { for(int j=0;j<output_l;++j) { int start_h = i*stride_h; int start_l = j*stride_l; int res = -0x3f3f3f3f; for(int ii=start_h;ii<start_h+chi_h;++ii) for(int jj=start_l;jj<start_l+chi_l;++jj) { res = max(res,data[ii][jj]); } out[i][j]=res; } } print_res(out,output_h,output_l); }