C语言代码:将2020年计数转换为日期和星期
#include <stdio.h>
struct Date { int year; int month; int day; int weekday; };
void calculateDate(int count, struct Date* date) { date->year = 2020; date->weekday = (count + 2) % 7; // 星期的计算公式 int daysInMonth[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 计算月份和日期
int i;
for (i = 0; i < 12; i++) {
if (count <= daysInMonth[i]) {
date->month = i + 1;
date->day = count;
break;
}
count -= daysInMonth[i];
}
}
int main() { int count; struct Date date;
printf('请输入一个1~366之间的数: ');
scanf('%d', &count);
calculateDate(count, &date);
printf('转换结果为:%d年%d月%d日 星期%d\n', date.year, date.month, date.day, date.weekday);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pbMr 著作权归作者所有。请勿转载和采集!