雷火笔试4.1 - 第四题
第四题我估摸着捣鼓了得一个半小时,害,本地调试我注视掉了cin,最后提交的时候忘了弄回来,结果0%case,气/-_-/
题目不难,就是逻辑太长,跟阅读理解似的。。。
#include <iostream>
#include <vector>
using namespace std;
class buttary{
public:
char m_type = 'X';// 1 2 3 4 5
int kick_count;
bool ele;
buttary *from = nullptr;
buttary *next1 = nullptr,*next2 = nullptr,*next3 = nullptr,*next4 = nullptr;
int m_index;
bool turn = false;
void init(char type,int index){
m_type = type;
kick_count = 0;
ele = false;
m_index = index;
// cout<<"add:"<<m_type<<m_index<<endl;
}
void add_from(buttary *i_from){
from=i_from;
}
void add_next(buttary *next){
// cout<<"add next"<<next->m_index<<endl;
if(next1==nullptr){
next1=next;
return;
}
if(next2==nullptr){
next2=next;
return;
}
if(next3==nullptr){
next3=next;
return;
}
if(next4==nullptr){
next4=next;
return;
}
}
void kick(){
// cout<<m_type<<" "<<m_index<<endl;
if(m_type=='D'){
if(ele==false && from->ele==true){
kick_count++;
}
ele = from->ele;
}else if(m_type=='C'){
if(turn){
ele = from->ele;
}else{
ele = false;
}
}else if(m_type=='B'){
// bool turn_to = turn;
if(ele==false&&from->ele){
turn=!turn;
// cout<<from->ele<<ele<<" "<<turn<<endl;
}
// if(turn==false&&turn_to==true)
// ele=false;
// else
// ele=from->ele;
// turn = turn_to;
if(turn){
ele=from->ele;
}else{
ele=false;
}
}
else{
ele = from->ele;
}
}
void turn_it(){
if(m_type!='C')return;
turn = !turn;
}
};
vector<buttary> button(256+1);
//递归实现
void kick_once(int index){
button[index].kick();
// cout<<"m_kick"<<index<<endl;
// cout<<"next:"<<button[index].next1->m_index;
if(button[index].m_type=='X'||button[index].next1==nullptr/*||button[index].next1->m_type=='X'*/)return;
if(button[index].next1!=nullptr)
kick_once(button[index].next1->m_index);
if(button[index].next2!=nullptr)
kick_once(button[index].next2->m_index);
if(button[index].next3!=nullptr)
kick_once(button[index].next3->m_index);
if(button[index].next4!=nullptr)
kick_once(button[index].next4->m_index);
}
int main(){
int N = 0;
cin>>N;
for(int i=0;i<N;i++){
buttary b_temp;
char type;
cin>>type;
b_temp.init(type,i+1);
button[i+1] = b_temp;
}
int M = 5;
cin>>M;
for(int i=0;i<M;i++){
int from,to;
cin>>from>>to;
button[from].add_next(&button[to]);
button[to].add_from(&button[from]);
}
//初始化,第一次通电
button[0].ele=true;
button[1].add_from(&button[0]);
kick_once(1);
int click = 5;
cin>>click;
for(int i=0;i<click;i++){
int click_idnex = 0;
cin>>click_idnex;
if(button[click_idnex].m_type=='C'){
button[click_idnex].turn_it();
kick_once(click_idnex);
}
}
for(int i=0;i<N;i++){
if(button[i+1].m_type=='D')
cout<<button[i+1].kick_count<<" ";
}
return 0;
} 各种中间件做成一个电源类,然后每次点击之后,递归往后查找,改变电路状态,然后注意脉冲信号和B类型的判断处理就没问题~
运行结果:


查看27道真题和解析