Nested Structs
Completed
Define Date (day, month, year) and Event (name, Date). Read n events and print the earliest as name YYYY-MM-DD.
Input
n, then n lines name day month year.
Output
One line.
Example
Input
3 launch 16 10 2026 meetup 2 3 2025 demo 30 3 2025
Output
meetup 2025-03-02
Editor
C
Hints
Access nested fields with two dots: e.date.year
Solution explanation
Comparing dates field by field from the most significant one is a common pattern.
Show solution
#include <stdio.h>
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
char name[30];
Date date;
} Event;
int earlier(Date *a, Date *b) {
if (a->year != b->year) return a->year < b->year;
if (a->month != b->month) return a->month < b->month;
return a->day < b->day;
}
int main() {
int n;
Event e[30];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%s %d %d %d", e[i].name, &e[i].date.day, &e[i].date.month, &e[i].date.year);
int best = 0;
for (int i = 1; i < n; i++) {
if (earlier(&e[i].date, &e[best].date)) best = i;
}
printf("%s %04d-%02d-%02d\n", e[best].name, e[best].date.year, e[best].date.month, e[best].date.day);
return 0;
}