Skip to content

Instantly share code, notes, and snippets.

@Bojne
Created November 8, 2015 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bojne/0b9898f8a04b5cf2ef57 to your computer and use it in GitHub Desktop.
Save Bojne/0b9898f8a04b5cf2ef57 to your computer and use it in GitHub Desktop.
C code
#include "stdio.h"
int main(void) {
int leap_year(int y)
{
int is_leap;
is_leap = (y % 400 == 0) ||
((y % 4 == 0) && !(y % 100 == 0));
return is_leap;
}
int how_many_days(int year, int month)
{
int days;
if (year < 0 || month < 1 || month > 12)
return 0;
switch (month)
{
case 1: case 3: case 5: case 7:
case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
days = leap_year(year)? 29 : 28;
break;
default:
days = 0;
}
return days;
}
scanf("%d %d", &inputY, &inputD);//第一行input:元旦時星期幾,範例 2015 4
int count(int inputY, inputD)
int targetM, targetD;
scanf("%d %d", &targetM, &targetD); //第二行input, 抓目標日期,範例 11 7
int day = 1;//初始化
int month = 1;//初始化
int today = inputD;
while(day < 32)//到兩個日期一樣時就會跳出的loop
today ++;
if (today >= 6)//如果一周到週六,重新開始
today = 0;
day++;
/*
if (day == how_many_days(inputY,month))//到了月底,進下個月
day=1;
month++;
*/
printf("%d", today);//最後丟出(過了多少天)今天星期幾
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment