zoukankan      html  css  js  c++  java
  • 给出年、月、日,计算该日是该年的第几天

    给出年、月、日,计算该日是该年的第几天

    题目解析:

    此题采用枚举法进行每月天数的累加,其中关键点注意需要判断年份是否为闰年,如果是还需要多累加1天。

    代码示例:

    #include <stdio.h>
    #include<stdio.h>
    
    /* 函数sum_day:计算日期 */
    int sum_day(int month, int day)        
    {
    	int day_tab[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    	int i;
    	for (i = 1; i < month; i++)
    		day += day_tab[i];      /* 累加所在月之前天数 */
    	return day;
    }                         
    
    /* 函数leap:判断是否为闰年 */
    int leap(int year)
    {
    	int leap;
    	leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    	return leap;
    }
    
    int main()
    {
    	int year, month, day, days;
    	printf("input date(year,month,day):");
    	scanf("%d %d %d", &year, &month, &day);
    	printf("%d/%d/%d ", year, month, day);
    	days = sum_day(month, day);                  /* 调用函数sum_day */
    	if (leap(year) && month >= 3)                  /* 调用函数leap */
    		days = days + 1;
    	printf("is the %dth day in this year.
    ", days);
    	return 0;
    }
    

    运行结果:

    给出年、月、日,计算该日是该年的第几天

  • 相关阅读:
    [UE4]九宫格图片拉伸
    [UE4]IsValid方法妙用
    [UE4]蓝图函数库
    [UE4]创建KillInfoPanel
    [UE4]条件语句Select
    [UE4]控件模板参数
    [UE4]控件模板
    [UE4]不推荐的UI更新方式
    [UE4]事件驱动的UI更新:事件调度器
    [UE4]更新UI的三种方式
  • 原文地址:https://www.cnblogs.com/inta/p/13362525.html
Copyright © 2011-2022 走看看