输出2个正整数,输出它们约数的并集
时间: 2024-04-30 16:23:19 浏览: 14
对于输入的两个正整数,假设分别为a和b,它们的约数的并集可以通过以下步骤得到:
1. 首先找出a和b的所有约数,分别存储到两个列表中。
2. 将两个列表合并,得到一个包含a和b所有约数的列表。
3. 对于列表中的每个元素,如果该元素在列表中出现的次数大于1,则只保留一个。
4. 将去重后的列表按照从小到大的顺序排序。
5. 输出排序后的列表即为a和b的约数的并集。
以下是Python代码实现:
```
a, b = map(int, input().split())
# 找出a和b的所有约数
divisors_a = [i for i in range(1, a + 1) if a % i == 0]
divisors_b = [i for i in range(1, b + 1) if b % i == 0]
# 合并两个列表
divisors = divisors_a + divisors_b
# 去重
divisors = list(set(divisors))
# 排序
divisors.sort()
# 输出
print(divisors)
```
例如,对于输入的a=12,b=18,输出为[1, 2, 3, 6, 9, 12, 18],即12和18的约数的并集。
相关问题
python编写程序,实现从键盘输入两个整数集合,输出两个集合的并集、交
Python中可以使用集合(set)来表示一个整数集合,并且集合提供了相应的操作方法,可以轻松实现从键盘输入两个整数集合,并输出它们的并集和交集。具体代码如下:
```python
# 从键盘输入第一个整数集合
input1 = input("请输入第一个整数集合(以空格分隔):").split()
set1 = set(map(int, input1))
# 从键盘输入第二个整数集合
input2 = input("请输入第二个整数集合(以空格分隔):").split()
set2 = set(map(int, input2))
# 计算并集和交集并输出
union = set1.union(set2) # 计算并集
intersection = set1.intersection(set2) # 计算交集
print("两个集合的并集为:", union)
print("两个集合的交集为:", intersection)
```
使用上述代码,用户在运行程序时即可通过键盘输入两个整数集合,并且程序会将输入解析并计算它们的并集和交集,最后输出结果。
例如,用户输入第一个集合为`1 2 3 4`,第二个集合为`3 4 5 6`,则程序会输出:
```
两个集合的并集为: {1, 2, 3, 4, 5, 6}
两个集合的交集为: {3, 4}
```
这样就实现了从键盘输入两个整数集合,并输出它们的并集和交集的程序。
c++语言实现单链表的交并并输出并集
以下是 C 语言实现单链表的交并并输出并集的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createList(int arr[], int n) {
Node *head = NULL, *tail = NULL;
for (int i = 0; i < n; i++) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = arr[i];
node->next = NULL;
if (head == NULL) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
void printList(Node *head) {
for (Node *p = head; p != NULL; p = p->next) {
printf("%d ", p->data);
}
printf("\n");
}
Node *listIntersection(Node *list1, Node *list2) {
Node *head = NULL, *tail = NULL;
for (Node *p1 = list1; p1 != NULL; p1 = p1->next) {
for (Node *p2 = list2; p2 != NULL; p2 = p2->next) {
if (p1->data == p2->data) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = p1->data;
node->next = NULL;
if (head == NULL) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
break;
}
}
}
return head;
}
Node *listUnion(Node *list1, Node *list2) {
Node *head = NULL, *tail = NULL;
for (Node *p1 = list1; p1 != NULL; p1 = p1->next) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = p1->data;
node->next = NULL;
if (head == NULL) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
}
for (Node *p2 = list2; p2 != NULL; p2 = p2->next) {
int found = 0;
for (Node *p = head; p != NULL; p = p->next) {
if (p2->data == p->data) {
found = 1;
break;
}
}
if (!found) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = p2->data;
node->next = NULL;
tail->next = node;
tail = node;
}
}
return head;
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {3, 4, 5, 6, 7};
Node *list1 = createList(arr1, 5);
Node *list2 = createList(arr2, 5);
Node *intersection = listIntersection(list1, list2);
Node *unionSet = listUnion(list1, list2);
printf("Intersection: ");
printList(intersection);
printf("Union: ");
printList(unionSet);
return 0;
}
```
希望这个代码能够帮助你实现单链表的交并并输出并集。
相关推荐
![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)