答案daanC语言

问题1:

#include <stdlib.h>

typedef struct {

int flag;

int row;

int col;

} UserInfo;

// 群信息:标识码 + 创建者中心位置 + 成员数

typedef struct {

int flag;

int centerRow;

int centerCol;

int count;

} Group;

static int *Resolve(const UserInfo *members, size_t membersSize, size_t *returnSize)

{

Group groups100; // 人数上限100,群数最多100

int groupCnt = 0;

for (size_t i = 0; i < membersSize; i++) {

int f = membersi.flag;

int r = membersi.row;

int c = membersi.col;

int found = -1;

// 从前往后找第一个同标识码且九宫格覆盖该点的群(即最早建立的)

for (int j = 0; j < groupCnt; j++) {

if (groupsj.flag == f &&

abs(r - groupsj.centerRow) <= 1 &&

abs(c - groupsj.centerCol) <= 1) {

found = j;

break;

}

}

if (found == -1) {

// 没有被覆盖,以本人位置为中心新建群

groupsgroupCnt.flag = f;

groupsgroupCnt.centerRow = r;

groupsgroupCnt.centerCol = c;

groupsgroupCnt.count = 1;

groupCnt++;

} else {

// 加入已存在的群

groupsfound.count++;

}

}

// 收集所有群的成员数量,按题目要求需 malloc 返回

int *result = (int *)malloc(sizeof(int) * (groupCnt > 0 ? groupCnt : 1));

for (int i = 0; i < groupCnt; i++) {

resulti = groupsi.count;

}

// 从大到小排序

for (int i = 0; i < groupCnt - 1; i++) {

for (int j = i + 1; j < groupCnt; j++) {

if (resulti < resultj) {

int tmp = resulti;

resulti = resultj;

resultj = tmp;

}

}

}

*returnSize = groupCnt;

return result;

}


问题2:

#include <stdlib.h>

#include <string.h>

typedef struct {

unsigned char *mem; // 内存数据

int size; // 总字节数

struct Block {

int start; // 块起始地址

int size; // 块大小

int used; // 1:已分配, 0:空闲

int appId; // 所属应用

} blocks256;

int blockCnt; // 当前块数量

} MemorySys;

// 16进制字符转数值

static int hexCharToInt(char c)

{

if (c >= '0' && c <= '9') return c - '0';

return c - 'A' + 10;

}

// 数值转16进制字符

static char intToHexChar(int v)

{

if (v < 10) return '0' + v;

return 'A' + v - 10;

}

// 初始化内存系统

static MemorySys *MemorySysCreate(const char *data)

{

MemorySys *sys = (MemorySys *)malloc(sizeof(MemorySys));

int len = (int)strlen(data);

sys->size = len / 2;

sys->mem = (unsigned char *)malloc(sys->size);

for (int i = 0; i < sys->size; i++) {

sys->memi = (hexCharToInt(datai \* 2) << 4) | hexCharToInt(datai \* 2 + 1);

}

// 初始整块空闲

sys->blockCnt = 1;

sys->blocks0.start = 0;

sys->blocks0.size = sys->size;

sys->blocks0.used = 0;

sys->blocks0.appId = -1;

return sys;

}

// 申请内存:从低地址开始找第一个足够大的连续空闲块

static int MemorySysApplyMem(MemorySys *sys, int appId, int memSize)

{

for (int i = 0; i < sys->blockCnt; i++) {

if (!sys->blocksi.used && sys->blocksi.size >= memSize) {

int start = sys->blocksi.start;

int oldSize = sys->blocksi.size;

if (oldSize > memSize) {

// 分裂:前部分分配,后部分空闲

for (int j = sys->blockCnt; j > i + 1; j--) {

sys->blocksj = sys->blocksj - 1;

}

sys->blocksi.size = memSize;

sys->blocksi.used = 1;

sys->blocksi.appId = appId;

sys->blocksi + 1.start = start + memSize;

sys->blocksi + 1.size = oldSize - memSize;

sys->blocksi + 1.used = 0;

sys->blocksi + 1.appId = -1;

sys->blockCnt++;

} else {

sys->blocksi.used = 1;

sys->blocksi.appId = appId;

}

return start;

}

}

return -1;

}

// 释放指定应用的所有内存块,并合并相邻空闲块

static void MemorySysFreeMem(MemorySys *sys, int appId)

{

for (int i = 0; i < sys->blockCnt; i++) {

if (sys->blocksi.used && sys->blocksi.appId == appId) {

sys->blocksi.used = 0;

sys->blocksi.appId = -1;

}

}

// 合并相邻空闲块

int i = 0;

while (i < sys->blockCnt - 1) {

if (!sys->blocksi.used && !sys->blocksi + 1.used) {

sys->blocksi.size += sys->blocksi + 1.size;

for (int j = i + 1; j < sys->blockCnt - 1; j++) {

sys->blocksj = sys->blocksj + 1;

}

sys->blockCnt--;

} else {

i++;

}

}

}

// 写内存:校验通过后写入4字节

static int MemorySysWriteData(MemorySys *sys, int verifyWord, int addr, const char *content)

{

int idx = -1;

for (int i = 0; i < sys->blockCnt; i++) {

if (sys->blocksi.used &&

addr >= sys->blocksi.start &&

addr < sys->blocksi.start + sys->blocksi.size) {

idx = i;

break;

}

}

if (idx == -1) return -1;

int start = sys->blocksidx.start;

int size = sys->blocksidx.size;

int appId = sys->blocksidx.appId;

int word = (33 * (33 * start + size + appId)) % 251;

if (word != verifyWord) return 0;

for (int i = 0; i < 4; i++) {

unsigned char byte = (hexCharToInt(contenti \* 2) << 4) |

hexCharToInt(contenti \* 2 + 1);

sys->memaddr + i = byte;

}

return 1;

}

// 查询完整内存数据,返回16进制字符串

static char *MemorySysQuery(MemorySys *sys)

{

char *res = (char *)malloc(sys->size * 2 + 1);

for (int i = 0; i < sys->size; i++) {

resi \* 2 = intToHexChar(sys->memi >> 4);

resi \* 2 + 1 = intToHexChar(sys->memi & 0xF);

}

ressys-\>size \* 2 = '\0';

return res;

}

// 释放内存系统

static void MemorySysFree(MemorySys *sys)

{

if (sys != NULL) {

free(sys->mem);

free(sys);

}

}


问题3:

#include <string.h>

typedef struct {

int nodeId;

int childId;

} Node;

#define MAX_NODES 1001

static int parentArrMAX_NODES;

static int childrenMAX_NODESMAX_NODES;

static int childCntMAX_NODES;

static int stateMAX_NODES;

static int nodeUsedMAX_NODES;

// 构建树,初始所有节点为未选中(0)

static void buildTree(const Node *dirTree, size_t dirTreeSize)

{

memset(parentArr, -1, sizeof(parentArr));

memset(childCnt, 0, sizeof(childCnt));

memset(state, 0, sizeof(state));

memset(nodeUsed, 0, sizeof(nodeUsed));

for (size_t i = 0; i < dirTreeSize; i++) {

int p = dirTreei.nodeId;

int c = dirTreei.childId;

nodeUsedp = 1;

nodeUsedc = 1;

parentArrc = p;

childrenpchildCnt\[p++] = c;

}

}

// 递归设置某节点及其所有子节点的状态

static void setSubtree(int node, int target)

{

statenode = target;

for (int i = 0; i < childCntnode; i++) {

setSubtree(childrennodei, target);

}

}

// 根据子节点状态向上更新父节点

static void updateParent(int node)

{

int p = parentArrnode;

if (p == -1) return;

int allSelected = 1;

int allUnselected = 1;

for (int i = 0; i < childCntp; i++) {

int s = statechildren\[pi];

if (s != 2) allSelected = 0;

if (s != 0) allUnselected = 0;

}

int newState = 1; // 半选

if (allSelected) newState = 2;

else if (allUnselected) newState = 0;

if (statep != newState) {

statep = newState;

updateParent(p);

}

}

static int SelectNodes(const Node *dirTree, size_t dirTreeSize, const int *operations, size_t operationsSize)

{

buildTree(dirTree, dirTreeSize);

for (size_t i = 0; i < operationsSize; i++) {

int op = operationsi;

// 选中(2)则取消,否则选中

int target = (stateop == 2) ? 0 : 2;

setSubtree(op, target);

updateParent(op);

}

int unselected = 0;

for (int i = 0; i < MAX_NODES; i++) {

if (nodeUsedi && statei == 0) {

unselected++;

}

}

return unselected;

}

相关推荐
ZhengEnCi5 小时前
大模型的分类到底怎么分:从基础模型到推理模型、从通用到专用的完整图谱
算法
取名真是6 小时前
Python 算法知识点从基础到进阶
开发语言·python·算法
罗西的思考6 小时前
【轻量化复刻大模型 / KIMI K3】KDA 模块交互式演示
人工智能·算法·机器学习
nm_zfyjt8 小时前
信息流广告投放优化实践:线索成本评估模型与转化链路重构(家装建材案例)
算法·重构·内蒙古朋友圈广告服务商·内蒙古朋友圈获客
w_zero_one9 小时前
算法--构造
java·数据结构·算法
其实防守也摸鱼10 小时前
信创安全竞赛知识总结与题库
大数据·网络·人工智能·算法·安全·php
小超人诞生日记11 小时前
C++ 智能指针的使用及其原理详解
c++·算法
lisw0513 小时前
互联网上的数据结构可视化!
数据结构·信息可视化
AiNightVision14 小时前
无人机夜视模块如何选择?——从参数指标到系统集成的完整选型指南
人工智能·算法·计算机视觉·车载系统·无人机·视频·智能硬件