UVA12148 题解

思路

总体思路

输入 n n n 以及 d i , m i , y i , n u m i d_i,m_i,y_i,num_i di,mi,yi,numi,用 c h e c k ( ) check() check() 函数依次判断前后两天是否相邻,如果是, a n s ← a n s + 1 , a n s 1 ← a n s 1 + ( n u m i − n u m i − 1 ) ans \gets ans + 1,ans1 \gets ans1 + (num_i - num_{i - 1}) ansans+1,ans1ans1+(numinumi1),最后输出 a n s , a n s 1 ans,ans1 ans,ans1

c h e c k ( ) check() check() 函数

显而易见,本体的难点是 c h e c k ( ) check() check() 函数怎么写。因为输入的日期合法,我们可以以此判断普通邻天,跨月以及跨年。

  • 普通邻天

显而易见,相邻两天必须满足 y i = y i − 1 , m i = m i − 1 y_i = y_{i - 1},m_i = m_{i - 1} yi=yi1,mi=mi1。因为输入保证前一个日期比后一个日期早,所以 d i − 1 + 1 = d i d_{i - 1} + 1 = d_i di1+1=di

  • 跨月
    y i − 1 = y i y_{i - 1} = y_i yi1=yi,并且其中一天是一个月的最后一天,另一天是下个月的第一天,则两天也相邻。所以我们还可以写出 m i − 1 + 1 = m i , d i = 1 m_{i - 1} + 1= m_i,d_i = 1 mi1+1=mi,di=1。这部分的难点在于如何判断 d i − 1 d_{i - 1} di1 是这个月的最后一天。我们可以在函数内开一个数组 m o n t h month month,储存每个月的最后一天如果 y i − 1 y_{i - 1} yi1 是闰年, m o n t h 2 ← m o n t h 2 + 1 month_2 \gets month_2 + 1 month2month2+1,并判断 d i − 1 d_{i - 1} di1 是否等于 m o n t h m i − 1 month_{m_{i - 1}} monthmi1 即可。

  • 跨年
    这部分也较为简单,只需要判断 y i − 1 + 1 = y i y_{i - 1} + 1 = y_i yi1+1=yi 且其中前一天是 12 月 31 号,第二天是 1 月 1 号即可。

代码

AC 记录

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n,d[5005],m[5005],y[5005],num[5005];
//闰年判断 
bool runnian_check(int x) {
	if(x % 400 == 0) return true;
	else if(x % 100 == 0) return false;
	else if(x % 4 == 0) return true;
	return false;
}
bool panduan(int x) {
	int month[15] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if(y[x - 1] == y[x]) {
		if(m[x - 1] == m[x]) {
			//普通邻天 
			if(d[x - 1] + 1 == d[x]) return true; 
		}
		//跨月 
		else if(m[x - 1] + 1 == m[x] and d[x] == 1) {
			if(runnian_check(y[x]))month[2]++;
			if(month[m[x - 1]] == d[x - 1]) return true;
		}
	}
	//跨年 
	else if(y[x - 1] + 1 == y[x] and m[x] == 1 and d[x] == 1 and m[x - 1] == 12 and d[x - 1] == 31) return true;
	return false;
}
signed main() {
	while(cin>>n) {
		if(n == 0) return 0;
		int ans = 0,ans1 = 0;
		for(int i = 1;i <= n;i++) scanf("%lld %lld %lld %lld",&d[i],&m[i],&y[i],&num[i]);
		for(int i = 2;i <= n;i++) {
			if(panduan(i)) {
				ans++,ans1 += num[i] - num[i - 1];
			}
		}
		printf("%lld %lld\n",ans,ans1);
	}
}
  • 21
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值