算法学习--Day2

今天要多学一些内容了,昨天就写了一点sort和struct的用法,今天写了两道关于日期的题目,记录在这里。

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入描述:

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出描述:

每组数据输出一行,即日期差值
示例1

输入

20110412
20110422

输出

11


#include <iostream>
#include <stdio.h>
#define isyear(x) x%100!=0 && x%4==0 || x%400==0?1:0
using namespace std;

int dayofMonth[13][2] = {
        0,0,
        31,31,
        28,29,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,

};

struct E{
    int day;
    int month;
    int year;
    void nextday(){
        day++;
        if(day>dayofMonth[month][isyear(year)]){
            day =1;
            month++;
            if(month>12){
                month=1;
                year++;
            }
        }
    }
};

int buf[5001][13][32];
int abs(int x){
    return x<0?-x:x;
}
int main(){
    E tmp;
    int cut = 0;
    tmp.year = 0;
    tmp.month = 1;
    tmp.day = 1;
    while (tmp.year!=5000){
        buf[tmp.year][tmp.month][tmp.day] = cut;
        tmp.nextday();
        cut++;
    }
    int y1,m1,d1,y2,m2,d2;
    while (scanf("%4d%2d%2d",&y1,&m1,&d1)!=EOF){
        scanf("%4d%2d%2d",&y2,&m2,&d2);
        printf("%d
",abs(buf[y2][m2][d2]-buf[y1][m1][d1])+1);
        
    }


    return 0;
}

这里我也稍微解释下这个代码,日期的问题书里确实处理的比较到位。他先将0~5000年12月31日的所有数据预处理一下,之后就可以直接拿来用了,很方便。

这里那个三维数组一定要定义到外面,不然就gg了。

第二题:

题目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入描述:

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出描述:

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
示例1

输入

9 October 2001
14 October 2001

输出

Tuesday
Sunday


#include <iostream>
#include <stdio.h>
#include <string.h>
#define isyear(x) x%100!=0 && x%4==0 || x%400==0?1:0
using namespace std;

int dayofMonth[13][2] = {
        0,0,
        31,31,
        28,29,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,

};
char Months[13][20]={
    "","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
}; 
char Weeks[7][20]={
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};

struct E{
    int day;
    int month;
    int year;
    void nextday(){
        day++;
        if(day>dayofMonth[month][isyear(year)]){
            day =1;
            month++;
            if(month>12){
                month=1;
                year++;
            }
        }
    }
};
int buf[3001][13][32];
int main(){
    E tmp;
    int cut = 0;
    tmp.year = 0;
    tmp.month = 1;
    tmp.day = 1;
    int days;
    int y1=2018,m1=4,d1=15,y2,d2;
    char S[20];
    while (tmp.year!=3001){
        buf[tmp.year][tmp.month][tmp.day] = cut;
        tmp.nextday();
        cut++;
    }
    int i;
    while (scanf("%d%s%d",&d2,S,&y2)!=EOF){
        for( i=1;i<=12;i++){
            if(strcmp(S,Months[i])==0){
                break;
            }
        }
         days = buf[y2][i][d2] - buf[2012][7][16];
         days+=1;
         cout<<Weeks[(days%7+7)%7]<<endl;
        
    }


    return 0;

}

第三题

题目描述

输入年、月、日,计算该天是本年的第几天。

输入描述:

包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。

输出描述:

输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1

输入

1990 9 20
2000 5 1

输出

263
122



#include <iostream>
#include <stdio.h>
#include <string.h>
#define isyear(x) x%100!=0 && x%4==0 || x%400==0?1:0

using namespace std;

int dayofMonth[13][2] = {
        0,0,
        31,31,
        28,29,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,

};
struct E{
    int year;
    int month;
    int day;
    void nextDay(){
        day++;
        if(day>dayofMonth[month][isyear(year)]){
            day=1;
            month+=1;
            if(month>12){
                month=1;
                year++;
            }
        }
    }
};
int buf[3001][13][32];
int main(){
    E tmp;
    int cut=0;
    tmp.year=1;
    tmp.month=1;
    tmp.day=1;
    int y1,m1,d1,y2;
    while(tmp.year!=3001){
        buf[tmp.year][tmp.month][tmp.day] = cut;
        tmp.nextDay();
        cut++;
    }
    while (scanf("%d%d%d",&y1,&m1,&d1)!=EOF){
        printf("%d
",buf[y1][m1][d1]-buf[y1][1][1]+1);
    }


    return 0;
}

第四题

题目描述

给出年分m和一年中的第n天,算出第n天是几月几号。

输入描述:

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出描述:

可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
示例1

输入

2000 3
2000 31
2000 40
2000 60
2000 61
2001 60

输出

2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01


#include <iostream>
#include <stdio.h>
#include <string.h>
#define isyear(x) x%100!=0 && x%4==0 || x%400==0?1:0

using namespace std;

int dayofMonth[13][2] = {
        0,0,
        31,31,
        28,29,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,

};

int main(){
    int y,data;
    while (scanf("%d%d",&y,&data)!=EOF){
        int current_y=data;
        int month_flag = isyear(y);
        int i=1;
        for( i=1;i<=12;i++){
            if(current_y - dayofMonth[i][month_flag]<0) break;
            current_y -= dayofMonth[i][month_flag];
        }
        printf("%d-%02d-%02d
",y,i,current_y);
    }

    return 0;
}

这里用到了下面的这个操作,输出整数用0占位,02代表用0占位两个数。

 printf("%d-%02d-%02d
",y,i,current_y);
原文地址:https://www.cnblogs.com/Pinging/p/8847901.html