HDU2005
来源:互联网 发布:ubuntu开机自启动脚本 编辑:程序博客网 时间:2024/05/27 03:26
第几天
Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
Solution
建立month[]打表,为了方便简洁,首先写了一个简单的程序,可以求出平年1月到i月的天数和,方便计算
#define Do double#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <iomanip>#include <stack>#include <vector>#include <queue>#include <cmath>#include <algorithm>using namespace std;int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};int main(){ int sum = 0; for (int i=1;i<13;++i){ sum += month [i]; printf("%d,",sum); } return 0;}
程序运行结果为:
31,59,90,120,151,181,212,243,273,304,334,365,
将其存入:
int month[13] ={0,31,59,90,120,151,181,212,243,273,304,334,365};
定义日期的结构体
struct Node{ int y; int m; int d;}a;
写出getday(Node)函数
int getday(Node date){ if (date.m>2) { return (month[date.m-1] + date.d + isleap(date.y)); } else{ return (month[date.m-1] + date.d); }}
程序如下:
#define Do double#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <iomanip>#include <stack>#include <vector>#include <queue>#include <cmath>#include <algorithm>using namespace std;int month[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};struct Node{ int y; int m; int d;}a;Node make_Node(int _y,int _m,int _d){ Node node; node.d = _d; node.m = _m; node.y = _y; return node;}inline bool isleap(int y){ return y%400==0||y%4==0&&y%100;}int getday(Node date){ if (date.m>2) { return (month[date.m-1] + date.d + isleap(date.y)); } else{ return (month[date.m-1] + date.d); }}int main(){ int y,m,d; while (scanf("%d/%d/%d",&y,&m,&d)!=EOF){ a = make_Node(y,m,d); printf("%d\n",getday(a)); }}
最后的结果:Accepted
Pay attention!
inline 虽然能够大幅度提高程序运行速度,但是inline也需要有返回值类型!
0 0
- HDU2005
- hdu2005
- HDU2005
- HDU2005
- HDU2005
- HDU2005
- hdu2005
- Hdu2005 **
- HDU2005
- HDU2005:第几天?
- hdu2005解题报告
- hdu2005,第几天?
- hdu2005第几天
- hdu2005第几天
- HDU2005 第几天?
- hdu2005 第几天?
- hdu2005第几天?
- HDU2005 第几天?
- BurpSuite系列(八)----decoder模块(编码模块)
- 线性表的抽象数据类型
- 循环单链表的实现java
- 基于数组对线性表的实现
- 区块链学习资源汇总
- HDU2005
- solr入门
- 【区块链之技术实战】区块链开源项目合集:Hello,BlockChain!
- nginx 配置 google代理
- MySQL2
- 负环图检测
- 大闹天竺、西游伏妖篇、免费电影资源
- LEAPMotion VR 各种手势的判断~
- 如何来实现一个Linux内核的系统调用(基于tiny4412开发板)