Giới thiệu
Cho số ngày, chuyển đổi thành năm, tuần, ngày. Giả sử 1 năm = 365 ngày, 1 tuần = 7 ngày.
Công thức
Năm = tổng_ngày / 365 Tuần = (tổng_ngày % 365) / 7 Ngày = (tổng_ngày % 365) % 7
Ví dụ
Input: 209 ngày → Output: 0 năm, 29 tuần, 6 ngày Input: 1000 ngày → Output: 2 năm, 38 tuần, 4 ngày
Thuật toán
Hàm find(total_days): year = total_days / 365 weeks = (total_days % 365) / 7 days = (total_days % 365) % 7 In year, weeks, days Hàm main: Total_days = 209 Gọi find()
Code C
#include <stdio.h>
const int DAYS_IN_WEEK = 7;
void find(int total_days) {
int year, weeks, days;
year = total_days / 365;
weeks = (total_days % 365) / DAYS_IN_WEEK;
days = (total_days % 365) % DAYS_IN_WEEK;
printf("years = %d\nweeks = %d\ndays = %d", year, weeks, days);
}
int main() {
int Total_days = 209;
find(Total_days);
return 0;
}
Kết quả
years = 0 weeks = 29 days = 6
Lưu ý
Chương trình không tính năm nhuận. Để chính xác hơn, có thể dùng thư viện <time.h>.