2302_80243748 2023-11-27 21:44 采纳率: 50%
浏览 6

如何用数组求出工期最大和最小值之差

img


第一行输入一个整数,表示可挑选的日期,第二行输入N个日期,每个日期的格式为年月,求输出最长的施工期的月份数量,先求出最小的日期和最大的日期再计算它们的月份之差。

  • 写回答

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;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月27日

悬赏问题

  • ¥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 工创大赛太阳能电动车项目零基础要学什么