黑色星期五

有一种计算机病毒叫黑色星期五,如果当天是13号,又恰好是星期五,就会发作起来毁坏计算机的存储系统,试编程找出九十年代中这种病毒可能发作的日期。

 1 #include <stdio.h>
 2 #define DAYBEGIN 1990 
 3 #define DAYEND   1999
 4 #define FRIDAY   5
 5 
 6 static day_table[2][13] = {
 7     0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
 8     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
 9 };
10 
11 int main()
12 {
13     int day, month, year;
14     int weekday;
15 
16     weekday = 1; //1990 1 1 is January 
17     printf("The black friday is : \n");
18     printf("year month day\n");
19     for (year = DAYBEGIN; year <= DAYEND; ++year) {
20         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
21             for (month = 1; month <= 12; month++) { 
22                 for (day = 1; day <= day_table[0][month]; day++, weekday++) {
23                     if (day == 13 && weekday == FRIDAY) //find the black friday 
24                         printf("%d   %2d   %d\n", year, month, day);    
25                     if (weekday == 7) // a week has gone
26                         weekday = 0;
27                 }
28             }
29         }
30         else {
31             for (month = 1; month <= 12; month++) { 
32                 for (day = 1; day <= day_table[1][month]; day++, weekday++) {
33                     if (day == 13 && weekday == FRIDAY) //find the black friday
34                         printf("%d   %2d   %d\n", year, month, day);
35                     if (weekday == 7) //a week has gone
36                         weekday = 0;
37                 }
38             }
39         }    
40     }
41     return 0;
42 }

测试结果如图:

posted on 2012-09-02 20:12  初级业余程序员  阅读(516)  评论(0编辑  收藏  举报

导航