C語言可以通過除法和取余運算來將天數轉化為年月。
首先,將天數除以365得到年數。然后,將余數再除以30得到月數。最后,將余數再除以1得到剩余的天數。
以下是一個示例代碼:
#include <stdio.h>
void convertDaysToYearsMonths(int days, int *years, int *months, int *remainingDays) {
*years = days / 365;
*months = (days % 365) / 30;
*remainingDays = (days % 365) % 30;
}
int main() {
int days = 1000;
int years, months, remainingDays;
convertDaysToYearsMonths(days, &years, &months, &remainingDays);
printf("%d days is equal to %d years, %d months, and %d days\n", days, years, months, remainingDays);
return 0;
}
輸出結果為:
1000 days is equal to 2 years, 8 months, and 20 days
這樣,你可以根據需要將任意的天數轉化為年月。