用c语言写一个姻缘测试,根据男女的年龄来判断两人是否合适 男性大于(等于)女性不超过10岁 ---------- 非常合适 女性大于男性不超过5岁 ----------- 再考虑一下 其他情况 --------------------- 不合适
时间: 2023-11-05 13:06:14 浏览: 36
以下是用C语言写的一个姻缘测试,根据男女的年龄来判断两人是否合适:
```c
#include <stdio.h>
int main() {
int male_age, female_age;
printf("请输入男性年龄:");
scanf("%d", &male_age);
printf("请输入女性年龄:");
scanf("%d", &female_age);
if (male_age >= female_age && male_age - female_age <= 10) {
printf("非常合适\n");
} else if (female_age > male_age && female_age - male_age <= 5) {
printf("再考虑一下\n");
} else {
printf("不合适\n");
}
return 0;
}
```
相关问题
用C语言来写一个如何判断两个链表是否相交的程序
以下是一个用C语言编写的判断两个链表是否相交的程序:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
int isIntersect(Node* list1, Node* list2) {
Node* p1 = list1;
Node* p2 = list2;
while (p1 != NULL) {
p2 = list2;
while (p2 != NULL) {
if (p1 == p2) {
return 1;
}
p2 = p2->next;
}
p1 = p1->next;
}
return 0;
}
int main() {
// 创建两个链表
Node* list1 = NULL;
Node* list2 = NULL;
for (int i = 1; i <= 5; i++) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = i;
newNode->next = NULL;
if (list1 == NULL) {
list1 = newNode;
} else {
newNode->next = list1;
list1 = newNode;
}
}
for (int i = 6; i <= 10; i++) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = i;
newNode->next = NULL;
if (list2 == NULL) {
list2 = newNode;
} else {
newNode->next = list2;
list2 = newNode;
}
}
// 将两个链表相交
Node* p = list1;
while (p->next != NULL) {
p = p->next;
}
p->next = list2->next->next;
// 判断两个链表是否相交
if (isIntersect(list1, list2)) {
printf("两个链表相交\n");
} else {
printf("两个链表不相交\n");
}
return 0;
}
用c语言写一个函数判断年份是否为闰年,并编写主函数测试
好的,以下是用C语言写一个函数判断年份是否为闰年的代码,同时也包含了一个主函数用于测试:
```c
#include <stdio.h>
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
int main() {
int year;
printf("请输入一个年份:");
scanf("%d", &year);
if (isLeapYear(year)) {
printf("%d年是闰年。", year);
} else {
printf("%d年不是闰年。", year);
}
return 0;
}
```
在上面的代码中,我们定义了一个函数`isLeapYear`,用于判断给定的年份是否为闰年。当年份是闰年时,函数返回1,否则返回0。这个函数的实现方式是利用闰年的定义,即能被4整除但不能被100整除的年份,或者能被400整除的年份都是闰年。
在主函数中,我们先输入一个年份,然后调用`isLeapYear`函数判断它是否为闰年,并输出相应的结果。