编程基本功——判断某天是一年中的第几天

一、分析

(1)首先分析是否为闰年,若为闰年则2月为29天

(2)其次计算之前的几个月一共有多少天

(3)最终加上该天在月中是多少天

二、源码

   1: #include <stdio.h>
   2:  
   3: int Day(int year, int month, int date)
   4: {
   5:     int months[13] = {0, 31, 0, 31, 30,
   6:                       31, 30, 31, 31, 30,
   7:                       31, 30, 31};
   8:     int i, days = 0;
   9:  
  10:     if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
  11:         months[2] = 29;
  12:     else
  13:         months[2] = 28;
  14:  
  15:     for (i = 1; i < month; ++i)
  16:     {
  17:         days += months[i];
  18:     }
  19:     days += date;
  20:     return days;
  21: }
  22:  
  23: int main()
  24: {
  25:     printf("the days of 25th May 2010 is %d\n", Day(2010, 5, 25));
  26:     return 0;
  27: }
posted @ 2010-05-25 08:59  红脸书生  阅读(3130)  评论(0)    收藏  举报