网易4个题答案
第一题
#include <iostream> #include <vector> #include <string> using namespace std; string getStr(long val){ int n = val; string ret; int tmp; while(n!=0){ tmp = n/2; if(tmp*2 == n){ ret += "0"; n = tmp; } else{ ret += "1"; n = tmp; } } return ret; } bool check(const string& s){ int left = 0; int right = s.size()-1; while(left < right){ if(s[left] == s[right]){ ++left;--right; continue; } else return false; } return true; } int main() { int n; cin >> n; vector<long> v(n); for(int i=0;i<n;++i) cin >> v[i]; vector<string> vs(n); for(int i=0;i<n;++i){ vs[i] = getStr(v[i]); if(check(vs[i])) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }第二题
#include <iostream> #include <vector> using namespace std; void check(const vector<vector<int>> &vv,int root, vector<int> &ret,int level){ if(ret.size() == level) ret.push_back(0); ret[level] += vv[root][0]; if(vv[root][1] != -1) check(vv,vv[root][1],ret,level+1); if(vv[root][2] != -1) check(vv,vv[root][2],ret,level+1); } int main() { int n; cin >>n; for(int i=0;i<n; ++i){ int m; cin >> m; vector<vector<int>> vv(m, vector<int>(4,-1)); int value,left,right; for(int i=0;i<m;++i){ cin>>value>>left>>right; vv[i][0] = value; vv[i][1] = left; vv[i][2] = right; if(left!=-1) vv[left][3] = i;// if(right!=-1) vv[right][3] = i;// } int root = -1; for(int i=0;i<m;++i){ if(vv[i][3] == -1){ root = i; } } if(root == -1){ cout<<"error"<<endl; return 0; } vector<int> ret; check(vv,root,ret,0); bool flag = true; for(int i=0;i<ret.size()-1;++i){ if(ret[i] >= ret[i+1]){ flag = false; break; } } if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
第三题
#include <iostream> #include <vector> #include <stdlib.h> using namespace std; int main() { int n; cin >> n; for(int i=0;i<n;++i){ int k,m; cin>>k>>m; vector<int> v(m); for(int j=0;j<m;++j) cin >> v[j]; vector<bool> v2(31,true); v2[0] = false; int ret = m; for(int j=0;j<m;++j){ int val = v[j]; int left = val-k; if(left<1) left = 1; int right = val+k; if(right > 30) right = 30; for(int z = left;z<=right;++z) v2[z] = false; } for(int j = 1; j<=30;++j){ if(v2[j] == false) continue; ++ret; int left = j-k; int right = j+k; if(left<1) left = 1; if(right>30) right = 30; for(int z = left;z<=right;++z) v2[z] = false; } cout<<ret<<endl; } return 0; }
第四题
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; bool check(vector<vector<bool>>& vv,vector<int>& board,bool val){ int row1 = board[0],col1 = board[1],row2 = board[2],col2 = board[3]; for(int i=row1;i<=row2;++i){ for(int j=col1;j<=col2;++j){ if(vv[i][j]!= val) return false; } } return true; } int main() { int u;// =1; cin >> u; for(int k=0;k<u;++k){ int n,m; cin >>n>>m; vector<vector<bool>> vv(n+1,vector<bool>(m+1)); /*int tmp; for(int i=1;i<n+1;++i){ for(int j=1;j<m+1;++j){ cin >>tmp; if(tmp) vv[i][j] = true; else vv[i][j] = false; } }*/ string s; for(int i=1;i<n+1;++i){ cin >> s; for(int j=1;j<m+1;++j){ if(s[j-1] == '1') vv[i][j] = true; else vv[i][j] = false; } } vector<int> ret{-1,-1,-1,-1}; bool finishFlag = false; int len = min(n,m)/3; for(int sz = len;sz>=1;--sz){ //int row = 1,col = 1; for(int row = 1;row-1+3*sz<=n;++row){ for(int col = 1;col-1+3*sz<=m;++col){ vector<vector<int>> vv2(9,vector<int>(4)); vv2[0][0] = row;vv2[0][1] = col; vv2[0][2] = row-1+sz; vv2[0][3] = col-1+sz; vv2[1][0] = row;vv2[1][1] = col+sz;vv2[1][2] = row-1+sz; vv2[1][3] = col-1+sz*2; vv2[2][0] = row;vv2[2][1] = col+sz*2;vv2[2][2] = row-1+sz; vv2[2][3] = col-1+sz*3; vv2[3][0] = row+sz;vv2[3][1] = col; vv2[3][2] = row-1+sz*2; vv2[3][3] = col-1+sz; vv2[4][0] = row+sz;vv2[4][1] = col+sz; vv2[4][2] = row-1+sz*2; vv2[4][3] = col-1+sz*2; vv2[5][0] = row+sz;vv2[5][1] = col+sz*2;vv2[5][2] = row-1+sz*2; vv2[5][3] = col-1+sz*3; vv2[6][0] = row+sz*2;vv2[6][1] = col; vv2[6][2] = row-1+sz*3;vv2[6][3] = col-1+sz; vv2[7][0] = row+sz*2;vv2[7][1] = col+sz; vv2[7][2] = row-1+sz*3;vv2[7][3] = col-1+sz*2; vv2[8][0] = row+sz*2;vv2[8][1] = col+sz*2;vv2[8][2] = row-1+sz*3;vv2[8][3] = col-1+sz*3; bool flag; flag = check(vv,vv2[0],false); if(!flag) continue; flag = check(vv,vv2[2],false); if(!flag) continue; flag = check(vv,vv2[6],false); if(!flag) continue; flag = check(vv,vv2[8],false); if(!flag) continue; flag = check(vv,vv2[1],true); if(!flag) continue; flag = check(vv,vv2[3],true); if(!flag) continue; flag = check(vv,vv2[4],true); if(!flag) continue; flag = check(vv,vv2[5],true); if(!flag) continue; flag = check(vv,vv2[7],true); if(!flag) continue; ret[0] = vv2[0][0];ret[1] = vv2[0][1];ret[2] = vv2[8][2];ret[3] = vv2[8][3]; finishFlag = true; break; } if(finishFlag == true) break; } if(finishFlag == true) break; } cout<<ret[0]<<" "<<ret[1]<<" "<<ret[2]<<" "<<ret[3]<<endl; } return 0; }
服务端开发工程师,就是咖啡,游乐园印章啥的题。