第一行输入一个整数,表示可挑选的日期,第二行输入N个日期,每个日期的格式为年月,求输出最长的施工期的月份数量,先求出最小的日期和最大的日期再计算它们的月份之差。
如何用数组求出工期最大和最小值之差
第一行输入一个整数,表示可挑选的日期,第二行输入N个日期,每个日期的格式为年月,求输出最长的施工期的月份数量,先求出最小的日期和最大的日期再计算它们的月份之差。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 真相重于对错 2023-11-28 09:53关注
输入不给你考虑
#include<iostream> #include<cstring> #include <vector> #include <stdio.h> #include <algorithm> using namespace std; struct Date { int year; int month; Date(int y=2000,int m=1):year(y),month(m){} bool operator<(const Date& rh) { if(year<rh.year) return true; else if (year == rh.year) { return month<rh.month; } else { return false; } } int operator-(const Date& rh){ Date t1=*this; Date t2=rh; int flag=1; if(t1<t2){ swap(t1,t2); flag=-1; } int dify=t1.year-t2.year; if(dify>1) dify=(dify-1)*12; else dify=0; int difm1=12-t2.month; int difm2=t1.month; return flag*(dify+difm1+difm2); } }; int main() { vector<Date> vec{{2019,6},{2018,7},{2020,12}}; sort(vec.begin(),vec.end()); int ans=*(vec.end()-1)-*vec.begin(); cout<<ans; }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
- ¥15 计算决策面并仿真附上结果
- ¥20 halcon 图像拼接
- ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
- ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
- ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
- ¥15 SPSS分类模型实训题步骤
- ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
- ¥15 求解决扩散模型代码问题
- ¥15 工创大赛太阳能电动车项目零基础要学什么