【华为OD】2024年C、D卷真题集:最新的真题集题库 C/C++/Java/python/JavaScript
【华为OD】2024年C、D卷真题集:最新的真题集题库 C/C++/Java/python/JavaScript-CSDN博客
JS、C、Java、python、C++代码实现:
【华为OD】D卷真题100分:VLAN资源池 JavaScript代码实现-CSDN博客
【华为OD】D卷真题100分:VLAN资源池 C语言代码实现-CSDN博客
【华为OD】D卷真题100分:VLAN资源池 Java代码实现-CSDN博客
【华为OD】D卷真题100分:VLAN资源池 python代码实现-CSDN博客
【华为OD】D卷真题100分:VLAN资源池 C++代码实现-CSDN博客
题目描述:
VLAN是一种对局域网设备进行逻辑划分的技术,为了标识不同的VLAN,引入VLAN ID(1-4094之间的整数)的概念。定义一个VLAN ID的资源池(下称VLAN资源池),资源池中连续的VLAN用开始VLAN-结束VLAN表示,不连续的用单个整数表示,所有的VLAN用英文逗号连接起来。现在有一个VLAN资源池,业务需要从资源池中申请一个VLAN,需要你输出从VLAN资源池中移除申请的VLAN后的资源池。
输入描述
第一行为字符串格式的VLAN资源池,第二行为业务要申请的VLAN,VLAN的取值范围为[1,4094]之间的整数。
输出描述
从输入VLAN资源池中移除申请的VLAN后字符串格式的VLAN资源池,输出要求满足题目描述中的格式,并且按照VLAN从小到大升序输出。
如果申请的VLAN不在原VLAN资源池内,输出原VLAN资源池升序排序后的字符串即可。
示例1
输入输出示例仅供调试,后台判题数据一般不包含示例
输入
1-5
2
输出
1,3-5
说明
原VLAN资源池中有VLAN 1、2、3、4、5,从资源池中移除2后,剩下VLAN 1、3、4、5,按照题目描述格式并升序后的结果为1,3-5。
示例2
输入输出示例仅供调试,后台判题数据一般不包含示例
输入
20-21,15,18,30,5-10
15
输出
5-10,18,20-21,30
说明
原VLAN资源池中有VLAN 5、6、7、8、9、10、15、18、20、21、30,从资源池中移除15后,资源池中剩下的VLAN为 5、6、7、8、9、10、18、20、21、30,按照题目描述格式并升序后的结果为5-10,18,20-21,30。
示例3
输入输出示例仅供调试,后台判题数据一般不包含示例
输入
5,1-3
10
输出
1-3,5
说明
原VLAN资源池中有VLAN 1、2、3,5,申请的VLAN 10不在原资源池中,将原资源池按照题目描述格式并按升序排序后输出的结果为1-3,5。
备注
输入VLAN资源池中VLAN的数量取值范围为[2-4094]间的整数,资源池中VLAN不重复且合法([1,4094]之间的整数),输入是乱序的。
wumu1024
+---+
3 | | ++ + +---|
| | | 3 + 6 + | + | +
| + | | + + + | + | +
| + | +---+ + + +++++ + + + | +
| + | + | + +----+ | | + + + | +
| + 3 | + | + + + 2 | | 2 + + + | +
| + | + | + + + | | + + + | +
| +---+ + | | | + ----+ | +---+ | | + | +
| | + | | | + | | | | | | + | +
| 1 | + | 8 | | + 1 | | | 1 | | 1 | | + | +
| | + | | | + | | | | | | | + | +
| +---+ + +---+ | ++---+ ++ +---+ +---+ | + | +
| | + | | | ++ | | |+ | +
|0 | + | 0 | 0 | ++ | 0 | |+ | +
| | + | | | ++ | | |+ | +
+---+ + +-------+ +---+| +|+ | +
+ + | +
0 1 2 3 4 5 6 7 8 9 10 11 12 + v: w u m u 1 0 2 4
题目解析:
按逻辑来处理即可,用到了布尔数组来处理
代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 4095
void update(int s, int e, int* arr, int arr_size) {
for (int i = s; i <= e && i < arr_size; ++i) {
arr[i] = 1;
}
}
void update_str(char* str, int* arr, int arr_size) {
char* token = strtok(str, ",");
while (token != NULL) {
char* dash = strchr(token, '-');
if (dash != NULL) {
int start = atoi(token);
int end = atoi(dash + 1);
update(start, end, arr, arr_size);
} else {
int idx = atoi(token);
arr[idx] = 1;
}
token = strtok(NULL, ",");
}
}
int output(int* arr, int arr_size, int i) {
int total = 1;
for (int j = i + 1; j < arr_size; ++j) {
if (arr[j]) {
++total;
} else {
return total;
}
}
return total;
}
int main() {
int arr[MAX_SIZE] = {0};
char input[MAX_SIZE];
char str[MAX_SIZE];
int id;
fgets(input, MAX_SIZE, stdin);
sscanf(input, "%s", str);
fgets(input, MAX_SIZE, stdin);
sscanf(input, "%d", &id);
update_str(str, arr, MAX_SIZE);
arr[id] = 0;
char out[MAX_SIZE];
int length = 0;
for (int i = 0; i < MAX_SIZE; ++i) {
if (arr[i]) {
int total = output(arr, MAX_SIZE, i);
if (total > 1) {
length += sprintf(out + length, "%d-%d,", i, i + total - 1);
i += total - 1;
} else {
length += sprintf(out + length, "%d,", i);
}
}
}
if (length > 0) {
out[length - 1] = '\0';
}
printf("%s\n", out);
return 0;
}