zoukankan      html  css  js  c++  java
  • SDUT2191Calendar

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2191

    题意:给你两个年月日,让你算出其中经历了多少天输出 。

    思路 :这个题是一个大大的模拟,可惜比赛的时候我都卡了一个半小时,又回去改了好久脑子都废了。今早上才在二师兄的点播之下才知道,原来年月日不一定上边的小,所以要判断一下,我就是因为这个WA了十几遍。。。。T_T。。。。。。。

    #include<stdio.h>
    #include<cmath>
    #include<iostream>
    
    using namespace std ;
    
    struct node
    {
        int year ;
        int month ;
        int date ;
    } ch,sh;
    
    int is_leap(int n)
    {
        if(n%400 == 0||(n%4==0&&n%100!=0))
            return 1 ;
        return 0 ;
    }
    
    int main()
    {
        int sum = 0 ;
        int mon[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31} ;
        scanf("%d:%d:%d",&ch.year,&ch.month,&ch.date) ;
        scanf("%d:%d:%d",&sh.year,&sh.month,&sh.date) ;
        if(ch.year > sh.year)
        {
            swap(ch.year,sh.year) ;
            swap(ch.month,sh.month) ;
            swap(ch.date,sh.date) ;
        }
    
        if(ch.year == sh.year)
        {
            if(ch.month > sh.month)
            {
                swap(ch.year,sh.year) ;
                swap(ch.month,sh.month) ;
                swap(ch.date,sh.date) ;
            }
            if(ch.month == sh.month)
                sum += fabs(sh.date-ch.date) ;
            else
            {
                for(int i = ch.month+1 ; i < sh.month ; i++)
                    sum += mon[i] ;
                sum += (mon[ch.month]+1-ch.date) ;
                sum += sh.date ;
                sum -= 1 ;
                if(is_leap(ch.year)&&ch.month <= 2&&sh.month > 2)
                    sum++ ;
            }
        }
        else
        {
            for(int i = ch.year+1 ; i <= sh.year-1 ; i++)
            {
                sum+=365 ;
                if(is_leap(i))
                    sum++ ;
            }
            if(ch.month != 12)
            {
                for(int i = ch.month+1 ; i <= 12 ; i++)
                    sum += mon[i] ;
                if(ch.month <= 2&&is_leap(ch.year))
                    sum ++ ;
            }
            sum += (mon[ch.month]+1-ch.date) ;
            if(sh.month!= 1)
            {
                for(int i = 1 ; i <= sh.month-1 ; i++)
                    sum+=mon[i]  ;
                if(sh.month > 2&&is_leap(sh.year))
                    sum++ ;
            }
            sum += sh.date-1 ;
        }
        printf("%d
    ",sum) ;
        return 0 ;
    }
    View Code

    当然还有一种做法,因为题目中是说年份是大于等于1900小于等于2038的,所以当你输入两个月份的时候,就以1900年01月01日为准,用两个年份分别算从1900年01月01日到他们的距离,再求差,当然了,因为上下两个年份不知道谁大谁小,所以要加绝对值,这种方法简单不易出错,代码量还小一半左右,想实现的可以试试哦

  • 相关阅读:
    Android Studio教程-创建第一个项目Hello World
    java打jar包的几种方式详解
    低学历者如何逆袭
    strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l, setlocale(LC_CTYPE, "Japanese_Japan")(MSDN的官方示例)
    select, poll, epoll
    KVM虚拟化知识的一些笔记
    在Windows系统上以C++打印出当前活动用户的环境变量
    日志组件解析
    Angular2案例rebirth开源
    视图
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3413667.html
Copyright © 2011-2022 走看看