今天要多学一些内容了,昨天就写了一点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[][] = {
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,, }; struct E{
int day;
int month;
int year;
void nextday(){
day++;
if(day>dayofMonth[month][isyear(year)]){
day =;
month++;
if(month>){
month=;
year++;
}
}
}
}; int buf[][][];
int abs(int x){
return x<?-x:x;
}
int main(){
E tmp;
int cut = ;
tmp.year = ;
tmp.month = ;
tmp.day = ;
while (tmp.year!=){
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\n",abs(buf[y2][m2][d2]-buf[y1][m1][d1])+); } return ;
}

这里我也稍微解释下这个代码,日期的问题书里确实处理的比较到位。他先将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[][] = {
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,, };
char Months[][]={
"","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
char Weeks[][]={
"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 =;
month++;
if(month>){
month=;
year++;
}
}
}
};
int buf[][][];
int main(){
E tmp;
int cut = ;
tmp.year = ;
tmp.month = ;
tmp.day = ;
int days;
int y1=,m1=,d1=,y2,d2;
char S[];
while (tmp.year!=){
buf[tmp.year][tmp.month][tmp.day] = cut;
tmp.nextday();
cut++;
}
int i;
while (scanf("%d%s%d",&d2,S,&y2)!=EOF){
for( i=;i<=;i++){
if(strcmp(S,Months[i])==){
break;
}
}
days = buf[y2][i][d2] - buf[][][];
days+=;
cout<<Weeks[(days%+)%]<<endl; } return ; }

第三题

题目描述

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

输入描述:

包括三个整数年(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[][] = {
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,, };
struct E{
int year;
int month;
int day;
void nextDay(){
day++;
if(day>dayofMonth[month][isyear(year)]){
day=;
month+=;
if(month>){
month=;
year++;
}
}
}
};
int buf[][][];
int main(){
E tmp;
int cut=;
tmp.year=;
tmp.month=;
tmp.day=;
int y1,m1,d1,y2;
while(tmp.year!=){
buf[tmp.year][tmp.month][tmp.day] = cut;
tmp.nextDay();
cut++;
}
while (scanf("%d%d%d",&y1,&m1,&d1)!=EOF){
printf("%d\n",buf[y1][m1][d1]-buf[y1][][]+);
} return ;
}

第四题

题目描述

给出年分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[][] = {
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,, }; int main(){
int y,data;
while (scanf("%d%d",&y,&data)!=EOF){
int current_y=data;
int month_flag = isyear(y);
int i=;
for( i=;i<=;i++){
if(current_y - dayofMonth[i][month_flag]<) break;
current_y -= dayofMonth[i][month_flag];
}
printf("%d-%02d-%02d\n",y,i,current_y);
} return ;
}

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

 printf("%d-%02d-%02d\n",y,i,current_y);

算法学习--Day2的更多相关文章

  1. DSP算法学习-过采样技术

    DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...

  2. 算法学习之C语言基础

    算法学习,先熟悉一下C语言哈!!! #include <conio.h> #include<stdio.h> int main(){ printf(+); getch(); ; ...

  3. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  4. C / C++算法学习笔记(8)-SHELL排序

    原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...

  5. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  6. 二次剩余Cipolla算法学习笔记

    对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...

  7. Manacher算法学习笔记 | LeetCode#5

    Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...

  8. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  9. PCA算法学习(Matlab实现)

    PCA(主成分分析)算法,主要用于数据降维,保留了数据集中对方差贡献最大的若干个特征来达到简化数据集的目的. 实现数据降维的步骤: 1.将原始数据中的每一个样本用向量表示,把所有样本组合起来构成一个矩 ...

随机推荐

  1. selenium python (九)对话框处理

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' from selenium import  webdriver #对于对 ...

  2. MapReduce将HDFS文本数据导入HBase中

    HBase本身提供了很多种数据导入的方式,通常有两种常用方式: 使用HBase提供的TableOutputFormat,原理是通过一个Mapreduce作业将数据导入HBase 另一种方式就是使用HB ...

  3. 【转载】C# sleep 和wait的区别

    eep和wait都是使线程暂时停止执行的方法,但它们有很大的不同. 1. sleep是线程类Thread 的方法,它是使当前线程暂时睡眠,可以放在任何位置. 而wait,它是使当前线程暂时放弃对象的使 ...

  4. wxWidgets刚開始学习的人导引(6)——wxWidgets学习材料清单

    wxWidgets刚開始学习的人导引全文件夹   PDF版及附件下载 1 前言2 下载.安装wxWidgets3 wxWidgets应用程序初体验4 wxWidgets学习资料及利用方法指导5 用wx ...

  5. yii第三方插件snoopy配置

    首先.把snoopy类放到protected\extensions\snoopy\目录下. 其次.在yii配置文件main.php里配置import扩展进来. 'import'=>array( ...

  6. linux 输入子系统(2) platform device

      Input  platform  device 一般是在板级bsp注册了的资源. 以gpio-keys为例: #####################gpio_key.h############ ...

  7. Java 线程池ThreadPoolExecutor简单应用

    什么是线程池: 线程池就是控制线程的一个池子.用来控制线程数量. 为什么要使用线程池: 1.避免系统反复创建和销毁线程影响执行的时间(创建+销毁>>执行时间时) 2.控制线程数量.线程过多 ...

  8. c++vector简单实现

    const int DEFAULT_CAP = 3; template <typename T> class vector { // int capacity; T* _data; int ...

  9. Eclipse中设置编码的方法

    Eclipse中设置编码的方式如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字 ...

  10. Window XP安装Ubuntu14.04实现Samba文件共享

    安装了Ubuntu14.04之后,在虚拟机设置里设置了文件共享.但在mnt文件夹下没有hgfs这个文件夹.依照网上说的去做还是不行,仅仅好放弃.改用samba实现Windows与Ubuntu文件共享. ...