C语言结构体数组的使用

小姐姐有n个相亲对象的信息(包括年龄、年收入、身高、体重),请你帮小姐姐找出全部满足以下条件的对象:
1.年龄小于27岁。 2年收入大于10万
3.身高170cm以上。 4体重120--140斤
输出所有满足以上条件的对象信息。

#include <stdio.h>

struct Person {
    int age;
    float income;
    int height;
    int weight; 
};

int main() {
    int n;
    scanf("%d", &n);
    
    struct Person persons[n];
    
    for (int i = 0; i < n; i++) {
        scanf("%d %f %d %d", &persons[i].age, &persons[i].income, &persons[i].height, &persons[i].weight);
    }
    
    for (int i = 0; i < n; i++) {
        if (persons[i].age < 27 && persons[i].income > 100 && persons[i].height >= 170 && persons[i].weight >= 120 && persons[i].weight <= 140) {
            printf("%d %f %d %d\n", persons[i].age, persons[i].income, persons[i].height, persons[i].weight);
        }
    }
}

使用结构体数组来存储相亲对象的信息,并使用循环遍历数组进行条件筛选。以下是一个示例代码:

#include <stdio.h>

#define MAX_SIZE 100

typedef struct {
    int age;
    int income;
    int height;
    int weight;
} Person;

void findMatches(Person people[], int n) {
    for (int i = 0; i < n; i++) {
        if (people[i].age < 27 && people[i].income > 100000 && people[i].height >= 170 && people[i].weight >= 120 && people[i].weight <= 140) {
            printf("满足条件的相亲对象 %d:年龄=%d岁,年收入=%d万元,身高=%dcm,体重=%d斤\n", i+1, people[i].age, people[i].income, people[i].height, people[i].weight);
        }
    }
}

int main() {
    int n;
    printf("请输入相亲对象的数量:");
    scanf("%d", &n);

    Person people[MAX_SIZE];

    printf("请输入每个人的信息:\n");
    for (int i = 0; i < n; i++) {
        printf("相亲对象 %d:\n", i+1);
        printf("年龄:");
        scanf("%d", &people[i].age);
        printf("年收入:");
        scanf("%d", &people[i].income);
        printf("身高:");
        scanf("%d", &people[i].height);
        printf("体重:");
        scanf("%d", &people[i].weight);
    }

    printf("满足条件的相亲对象信息如下:\n");
    findMatches(people, n);

    return 0;
}


这段代码通过结构体数组 people 存储相亲对象的信息,然后使用 findMatches 函数遍历数组并筛选满足条件的对象进行输出。main 函数用于接收用户输入的相亲对象数量和信息,然后调用 findMatches 函数进行匹配并输出结果。请根据实际需求进行适当修改和调整。