题解 | #火眼金睛#
火眼金睛
https://www.nowcoder.com/practice/d311403b15b8495b81b622edaefd5b5a
/**
* @nc app=nowcoder id=d311403b15b8495b81b622edaefd5b5a topic=182 question=34666 lang=Typescript
* 2024-01-09 16:36:28
* https://www.nowcoder.com/practice/d311403b15b8495b81b622edaefd5b5a?tpId=182&tqId=34666
* [SG1] 火眼金睛
*/
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//变量定义
let count = 0, T = 0, N = 0, Next = false, Arr = [], arr: Number[][] = [], ID = new Set(), Category = new Set();
let QAset: Object[] = [];
const flatten=(arr: Number[][]) => {
arr.forEach(item => {
let Q:Number = 0, QSc:Number = 0;
item.forEach((itm,index) => { //行遍历
if(index == 0) Q = itm;
else if(index == 1) QSc = itm;
else if(index > 1){
if(Q !== itm) //自己给自己出题
{
const obj = {[Q as number]: itm };
if(QAset.find(test =>(parseInt(test[Object.keys(test)[0]]) == itm
&& parseInt(Object.keys(test)[0]) == Q)
) == undefined ) QAset.push(obj)
} //添加QA映射对
}
})
})
}
// 先做对象去重 key1 = value2 && key2 = value1
const judge_1 = (arr: Object[]) => {
for(const item_a of arr){
for(const item_b of arr){
if( item_a[Object.keys(item_a)[0]] == Object.keys(item_b)[0]
&& item_b[Object.keys(item_b)[0]] == Object.keys(item_a)[0]
&& item_a[Object.keys(item_a)[0]] !== Object.keys(item_a)[0]
&& item_a[Object.keys(item_b)[0]] !== Object.keys(item_b)[0]){
ID.add(Object.keys(item_a)[0]);
ID.add(Object.keys(item_b)[0]);
}
Category.add(Object.keys(item_a)[0]);
}
}
}
//找到共值的作弊者的出題人
const judge_2 = (arr: Object[]) => {
Category.forEach(item => {
let count = 0; //计数器 记录都是这个人出题的
arr.forEach(itm => {
if(Object.keys(itm)[0] == item) count ++;
})
if(count > 1) ID.add(item);
})
}
rl.on('line', function (line) {
if((line.split(' ').length == 1 && count >= N) || Next) //确定是行
{
count = 0
Next = false
arr = []
N = parseInt(line) //第一行为数组行数
T ++ //第T组数据
}
count ++
if( 0 < count && count <= N + 1)
{
arr.push(line.split(' ')) //写入数组
// console.log("count:",count,arr)
}
if(count == N + 1) {
Arr.push(arr as never); //压入数组
Next = true
}
});
rl.on('close', ()=>{
let flag = 0;
while(flag< T)
{
QAset = [];
ID.clear();
Category.clear();
flatten(Arr[flag]);
flag++;
judge_1(QAset);
judge_2(QAset);
let mans = "";
Array.from(ID)
.sort((a,b) => parseInt(a as string) - parseInt(b as string))
.forEach(item => {
mans += item + " ";
})
if(ID.size !== 1)
{
console.log(ID.size); //所有作弊者数量
mans ? console.log(mans) : null //所有作弊者ID
}
else console.log(0);
}
})
总结一下这道题要考虑到的地方:
- 复杂度 现在只能o^2
- 键值对索引的时候返回是数组 要有下标
- 在答案匹配有特殊情況要考慮 一个人重复回答自己的问题回答多次一个人重复回他其他人的问题作弊至少是2个人 不会存在一个人自己作弊的情况不只是一轮 是多轮输入 需要通过滑动窗口处理


影石Insta360公司氛围 452人发布

查看10道真题和解析