给定一个集合,查找集合中一共多多少种不同的元素
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/28 01:01:11
给定一个集合,查找集合中一共多多少种不同的元素
第一行输入一个n,表示集合元素的个数
接下来输入n的整数
输入集合中一共有多少个不同的值
尽量使用高效算法
第一行输入一个n,表示集合元素的个数
接下来输入n的整数
输入集合中一共有多少个不同的值
尽量使用高效算法
楼上其实用的是hash的思想,不过人为加了很多条件哦,
我来给个基于hash的简单实现吧~
/* 程序思路:
* 将输入的n个数字散列到哈希表中,建立哈希表的过程中,
* 查找有没有重复数字,如果有,则总个数值不变
* 否则总个数+1
* */
#include
#include
#define MAX 8192
struct hash_node {
int data;
struct hash_node *next;
};
int hash_num(int num)
{
static struct hash_node table[MAX];
struct hash_node *q = NULL;
struct hash_node *p = NULL;
int index;
index = abs(num) % MAX;
p = &table[index];
while (p->next != NULL) {
if (p->next->data == num) {
q = malloc(sizeof(struct hash_node));
q->data = num;
q->next = p->next->next;
p->next->next = q;
return 1;
}
p = p->next;
}
if (p->next == NULL) {
p->next = malloc(sizeof(struct hash_node));
p->next->data = num;
p->next->next = NULL;
return 0;
}
}
int main()
{
int n = 0;
int i = 0;
int num = 0;
int cnt = 0;
printf("Please input n:");
scanf("%d",&n);
printf("Please input n nums:\n");
for (i = 0; i < n; ++i) {
scanf("%d",&num);
if (hash_num(num) == 0)
cnt++;
}
printf("%d",cnt);
return 0;
}
我来给个基于hash的简单实现吧~
/* 程序思路:
* 将输入的n个数字散列到哈希表中,建立哈希表的过程中,
* 查找有没有重复数字,如果有,则总个数值不变
* 否则总个数+1
* */
#include
#include
#define MAX 8192
struct hash_node {
int data;
struct hash_node *next;
};
int hash_num(int num)
{
static struct hash_node table[MAX];
struct hash_node *q = NULL;
struct hash_node *p = NULL;
int index;
index = abs(num) % MAX;
p = &table[index];
while (p->next != NULL) {
if (p->next->data == num) {
q = malloc(sizeof(struct hash_node));
q->data = num;
q->next = p->next->next;
p->next->next = q;
return 1;
}
p = p->next;
}
if (p->next == NULL) {
p->next = malloc(sizeof(struct hash_node));
p->next->data = num;
p->next->next = NULL;
return 0;
}
}
int main()
{
int n = 0;
int i = 0;
int num = 0;
int cnt = 0;
printf("Please input n:");
scanf("%d",&n);
printf("Please input n nums:\n");
for (i = 0; i < n; ++i) {
scanf("%d",&num);
if (hash_num(num) == 0)
cnt++;
}
printf("%d",cnt);
return 0;
}
给定一个集合,查找集合中一共多多少种不同的元素
给定一个集合,查找元素是否在集合中出现.求C语言算法
求帮忙弄个C课程设计,只要程序就行,急用!给定一个集合,查找元素是否在集合中出现
在Matlab中查找集合中元素的位置
“对于一个给定的集合,集合中的元素是互异的”(第2页),
排列组合:给定n个相同的集合,每个集合中有m个元素,从每个集合中任意选一元素,这些元素的组合数是多少
一个集合由8个不同元素组成,这个集合中含3个元素的子集有多少个?
对于给定的一个集合,集合中的元素是互易的.谁能帮我讲一下这句话啊
集合中元素与集合的关系
1 在给定的一个集合中,里面有元素,没有空集这个元素,但是做题目的时候要考虑到吗?
lingo 一个集合中所有相等的不同元素按表达式e计算怎样表示?
集合中可以有不同性质的元素么?