Python 用c语言制作日历
我写了一个根据公历打印日历的代码。但是我有一个问题,我需要计算当前的周数,以便在数组[5][7]的帮助下打印日历。我需要公式或程序来找到前一年的周数:2020年月:4天:26,我应该找到周数,它是4。有什么公式吗?你能不能得到从1月1日开始的天数,用7除以这个数字,再加上1(所以它从第1周开始,而不是0) 例如: 1月17日:17/7=2->2+1=第3周Python 用c语言制作日历,python,c,printing,calendar,formula,Python,C,Printing,Calendar,Formula,我写了一个根据公历打印日历的代码。但是我有一个问题,我需要计算当前的周数,以便在数组[5][7]的帮助下打印日历。我需要公式或程序来找到前一年的周数:2020年月:4天:26,我应该找到周数,它是4。有什么公式吗?你能不能得到从1月1日开始的天数,用7除以这个数字,再加上1(所以它从第1周开始,而不是0) 例如: 1月17日:17/7=2->2+1=第3周 对于第7天、第14天、第21天、第28天等,您可以查看剩余的。如果余数为0,则周应为第1周。因此,第14天是第2周,而不是第3周。你必须先知
对于第7天、第14天、第21天、第28天等,您可以查看剩余的。如果余数为0,则周应为第1周。因此,第14天是第2周,而不是第3周。你必须先知道一周中的哪一天 为此,请使用高斯算法Kraitchik变异: 下面是一个适用于我的代码示例:
#include <stdio.h>
#include <stdint.h>
static const int days[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const char* dayofws[] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
static int dayofweek(int y, int m, int d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
int k = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
return k == 0 ? 6 : (k-1); // This will return 0 for monday...6 for sunday
}
void main (void)
{
int y = 2020;
for (int m = 1; m <= 12; m++)
{
for (int d = 1; d <= days[m]; d++)
{
int w = (d - 1 + dayofweek(y, m, 1))/7 + 1;
printf("%d ", dayofweek(y, m, 1));
printf("%d/%02d/%02d : %d (%s)\n", y, m, d, w, dayofws[dayofweek(y, m, d)]);
}
printf("\n");
}
}
#包括
#包括
静态常数int天[]={0,31,29,31,30,31,30,31,31,30,31,30,30,31};
静态常量char*dayofws[]={“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”、“星期日”};
静态int星期(int y,int m,int d)/* 1使用找到的公式。这可以帮助:你所说的是真的,但是我认为日历可以从星期二开始或其他的日历。我的意思是,如果那样的话,那就不会是真的。你是对的,我没有完全想清楚这有点复杂。。。检查一下这个:它是w=(d-1+dayofweek(y,m,1))/7+1;我更新了我的答案。
w = (d - 1 + dayofweek(y, m, 1))/7 + 1;
#include <stdio.h>
#include <stdint.h>
static const int days[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const char* dayofws[] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
static int dayofweek(int y, int m, int d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
int k = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
return k == 0 ? 6 : (k-1); // This will return 0 for monday...6 for sunday
}
void main (void)
{
int y = 2020;
for (int m = 1; m <= 12; m++)
{
for (int d = 1; d <= days[m]; d++)
{
int w = (d - 1 + dayofweek(y, m, 1))/7 + 1;
printf("%d ", dayofweek(y, m, 1));
printf("%d/%02d/%02d : %d (%s)\n", y, m, d, w, dayofws[dayofweek(y, m, d)]);
}
printf("\n");
}
}