用c语言写一个程序:有若干串长度为n的整数,现要求使用数据结构算法编写程序,若这串数中存在值相同的数, 则只保留一个,最终按照原来的顺序将这串数输出
时间: 2023-02-14 13:29:23 浏览: 51
我可以给您提供一个示例程序,该程序使用哈希表来实现上述功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_N 10000
int n;
int a[MAX_N];
int hash[MAX_N]; // 哈希表,用于判重
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
if (hash[a[i]] == 0) { // 若该数没有出现过
printf("%d ", a[i]); // 输出该数
hash[a[i]] = 1; // 标记该数已出现过
}
}
return 0;
}
```
上面的程序会读入一个整数n,然后读入n个整数并存入数组a中。接着,程序使用哈希表来记录每个数字是否出现过,遍历数组a,如果某个数字没有出现过,就将其输出。
请注意,上面程序中的哈希表hash只能处理范围内的整数,若需要处理更大范围的整数,需要使用更高效的哈希函数或其它数据结构。
相关问题
用c语言写一个程序:有若干串长度为n的整数,现要求使用顺序表的方法编写程序,若这串数中存在值相同的数, 则只保留一个,最终按照原来的顺序将这串数输出
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct SeqList {
int data[MAX_SIZE];
int length;
} SeqList;
SeqList* init_seq_list() {
SeqList *l = (SeqList*)malloc(sizeof(SeqList));
l->length = 0;
return l;
}
void push_back(SeqList *l, int value) {
l->data[l->length++] = value;
}
int find(SeqList *l, int value) {
for (int i = 0; i < l->length; i++) {
if (l->data[i] == value) {
return i;
}
}
return -1;
}
void remove_at(SeqList *l, int index) {
for (int i = index; i < l->length - 1; i++) {
l->data[i] = l->data[i + 1];
}
l->length--;
}
int main() {
SeqList *l = init_seq_list();
int n;
printf("请输入长度n:");
scanf("%d", &n);
printf("请输入 %d 个整数:\n", n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
push_back(l, x);
}
for (int i = 0; i < l->length; i++) {
int value = l->data[i];
int index = find(l, value);
if (index != i) {
remove_at(l, index);
i--;
}
}
printf("去重后的数组: ");
for (int i = 0; i < l->length; i++) {
printf("%d ", l->data[i]);
}
printf("\n");
free(l);
return 0;
}
```
说明:
- `init_seq_list` 函数用于初始化顺序表
- `push_back` 函数用于在顺序表尾部插入一个整数
- `find` 函数用于在顺序表中查找给定值的第一个下标
- `remove_at` 函数用于删除顺序表中给定下标的元素
程序的主要思路是遍历顺序表,如果当前元素的
用c语言写程序:编写程序:输入n个整数,输出最大值
好的,以下是用 C 语言编写的程序,可以输入 n 个整数,然后输出它们中的最大值:
```c
#include <stdio.h>
int main() {
int n, max;
printf("请输入整数个数:");
scanf("%d", &n);
printf("请输入 %d 个整数:\n", n);
scanf("%d", &max);
for (int i = 1; i < n; i++) {
int num;
scanf("%d", &num);
if (num > max) {
max = num;
}
}
printf("最大值为:%d\n", max);
return 0;
}
```
运行程序后,按照提示输入整数个数和具体的整数,程序会输出它们中的最大值。
例如,输入 5 个整数 4、1、9、5、7,程序会输出:
```
最大值为:9
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)