gpt4 book ai didi

c - 如何从我生成的随机数组中删除重复元素?

转载 作者:行者123 更新时间:2023-11-30 14:58:03 24 4
gpt4 key购买 nike

所以我有这个代码:

#include <stdio.h>
#include <stdlib.h>

int main() {

char a[200], b = 0;
int x;
x = 100 + rand() % 200;
for (int i = 0; i < x; i++) {
a[i] = 'a' + rand() % 26;
}

for (int i = 0; i < x; i++) {
if (b % 10 == 0) {
printf("\n%c ", a[i]);
b = 0;
}
else {
printf("%c ", a[i]);
}
b++;
}
printf("\n");

return 0;

}

目的是我应该生成从“a”到“z”的随机字母数组(我已经设法做到了),然后打印新数组,而没有在第一个数组中重复的元素。我尝试实现 here 中的代码用于删除重复元素,但它在我的代码中不起作用。

最佳答案

一个简单的解决方案是循环遍历数组并将每个元素复制到新数组,首先检查该值是否已存在于新数组中。

关于c - 如何从我生成的随机数组中删除重复元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43497858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com