奇信安笔试1.63
第一题比较简单,但是老夫一万年没写过c++了啊,写了第二题现搜的c++怎么读输入,ac了
#include<iostream> #include<math.h> using namespace std; int main(int argc,char**argv) { int m,n,k; cin>>m; int i = 0; int total = (int)pow(2.0, (double)m) - 1; int r[total+1]; for(;i< total;i++) cin>>r[i+1]; cin>>n; cin>>k; int n_i = 1; int m_i = 1; while(r[n_i]!=n){ if(r[n_i]<n){ n_i = n_i * 2 + 1; }else{ n_i = n_i* 2; } if(n_i>=total){ cout << -1; return 0; } } while(r[m_i]!=k){ if(r[m_i]<k){ m_i *= 2; m_i += 1; }else{ m_i *= 2; } if(m_i>total){ cout << -1; return 0; } } int max_ = 1; int min_ = 1; if(m_i == n_i){ cout << r[m_i] << endl; return 0; } if(m_i > n_i){ max_ = m_i; min_ = n_i; }else{ max_ = n_i; min_ = m_i; } while(max_ > min_){ max_ /= 2; if(max_ == min_){ cout << r[min_] << endl; return 0; } } while(1){ if(max_ == min_){ cout << r[min_] << endl; return 0; } if(max_ < min_){ min_ /= 2; }else{ max_ /= 2; } } return 0; }
第二题就很恶心了,对了63%,剩下的运行时错误了,很烦。
from collections import defaultdict, Counter from queue import deque import sys line = sys.stdin.readline() lines = sys.stdin.readlines() l = [] for i in range(len(lines)-1): l.append(list(map(int, lines[i].strip().split()))) init = list(map(int, lines[-1].strip().split())) from_which = defaultdict(set) q = deque() visited = set() for i in init: q.append(i) visited.add(i) from_which[i].add(i) while q: cur = q.popleft() for i, v in enumerate(l[cur]): if v == 1 and i != cur: from_which[i] = from_which[i].union(from_which[cur]) if i not in visited: q.append(i) visited.add(i) result = [] for k in from_which: if len(from_which[k]) == 1: result.append(list(from_which[k])[0]) # init = list(filter(lambda x: len(from_which[x]) == 1, init)) c = Counter(result) r = 0 m = 0 for i in init: if len(from_which[i]) == 1 and c[i] > m: r = i m = c[i] print(r)