Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Five-weekends/C/five-weekends-1.c
Normal file
30
Task/Five-weekends/C/five-weekends-1.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
static const char *months[] = {"January", "February", "March", "April", "May",
|
||||
"June", "July", "August", "September", "October", "November", "December"};
|
||||
static int long_months[] = {0, 2, 4, 6, 7, 9, 11};
|
||||
|
||||
int main() {
|
||||
int n = 0, y, i, m;
|
||||
struct tm t = {0};
|
||||
printf("Months with five weekends:\n");
|
||||
for (y = 1900; y <= 2100; y++) {
|
||||
for (i = 0; i < 7; i++) {
|
||||
m = long_months[i];
|
||||
t.tm_year = y-1900;
|
||||
t.tm_mon = m;
|
||||
t.tm_mday = 1;
|
||||
if (mktime(&t) == -1) { /* date not supported */
|
||||
printf("Error: %d %s\n", y, months[m]);
|
||||
continue;
|
||||
}
|
||||
if (t.tm_wday == 5) { /* Friday */
|
||||
printf(" %d %s\n", y, months[m]);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d total\n", n);
|
||||
return 0;
|
||||
}
|
||||
40
Task/Five-weekends/C/five-weekends-2.c
Normal file
40
Task/Five-weekends/C/five-weekends-2.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int check_month(int y, int m)
|
||||
{
|
||||
char buf[1024], *ptr;
|
||||
int bytes, *a = &m;
|
||||
|
||||
sprintf(buf, "ncal -m %d -M %d", m, y);
|
||||
FILE *fp = popen(buf, "r");
|
||||
if (!fp) return -1;
|
||||
|
||||
bytes = fread(buf, 1, 1024, fp);
|
||||
fclose(fp);
|
||||
buf[bytes] = 0;
|
||||
|
||||
#define check_day(x) \
|
||||
ptr = strstr(buf, x);\
|
||||
if (5 != sscanf(ptr, x" %d %d %d %d %d", a, a, a, a, a)) return 0
|
||||
|
||||
check_day("Fr");
|
||||
check_day("Sa");
|
||||
check_day("Su");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int y, m, cnt = 0;
|
||||
for (y = 1900; y <= 2100; y++) {
|
||||
for (m = 1; m <= 12; m++) {
|
||||
if (check_month(y, m) <= 0) continue;
|
||||
printf("%d-%02d ", y, m);
|
||||
if (++cnt % 16 == 0) printf("\n");
|
||||
}
|
||||
}
|
||||
printf("\nTotal: %d\n", cnt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue