1008. 二哥买期货

Description

二哥想知道在一段时期内,一共有多少个交易日。期货交易日的限定如下:

  1. 周六、周日不能交易
  2. 元旦期间(1月1日)不能交易
  3. 五一劳动节期间(5月1日至3日)不能交易
  4. 十一国庆节期间(10月1日至7日)不能交易
  5. 没有在上述要求中提到的日期均可交易

Input Format

第一行有一个整数n,表示一共有n组数据。

每组数据都有一行,是两个用空格分开的日期,分别为开始日期和结束日期。日期格式为YYYY-MM-DD(比如2010-11-11);数据保证开始日期不晚于结束日期。

对于所有数据:n365

对于30%的数据:日期范围从2010-11-23至2012-12-21

对于70%的数据:日期范围从1900-01-01至9999-12-31

Output Format

输出共n行,每行一个整数,对应于一组数据。

每组数据需要输出在指定日期区间内,共有多少个交易日;区间的开始和结束日期也算在内(如果是交易日的话)。

Sample Input

4
2010-11-18 2010-11-20
2010-01-01 2010-01-01
2010-05-01 2010-05-03
2010-10-01 2010-10-07

Sample Output

2
0
0
0




#include<iostream>
#include<cstring>
using namespace std;

int isyeap(int x){
    return x%100!=0 && x%4==0 || x%400==0 ? 1 :0 ;
}

int dayofmonth[13][2]={
   0,0,
   31,31,
   28,29,
   31,31,
   30,30,
   31,31,
   30,30,
   31,31,
   31,31,
   30,30,
   31,31,
   30,30,
   31,31,
};

struct date{
    int day;
    int month;
    int year;
    int week;
    void nextday(){
        day++;
        week++;
        if(week>7){
            week=1;
        }
        if(day>dayofmonth[month][isyeap(year)]){
            day=1;
            month++;
            if(month>12){
                month=1;
                year++;
            }
        }
    }
};

int buf[10001][13][32];

int weekday(int y,int m,int d){
    if(m==1 || m==2){
        m+=12;
        y--;
    }
    return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
}

void init(){
    date temp;
    int cnt=0;
    temp.day=1;
    temp.month=28;
    temp.year=1899;
    temp.week=12;
    while(temp.year!=10001){
        buf[temp.year][temp.month][temp.day]=cnt;
        temp.nextday();
        if(temp.week==6 || temp.week==7) continue;
        if(temp.month==1 && temp.day==1) continue;
        if(temp.month==5 && temp.day<=3 ) continue;
        if(temp.month==10 && temp.day<=7) continue;
        cnt++;
    }
}

int main(){
    init();
    //freopen("input.txt","r",stdin);
    int n;
    cin>>n;
    string s;
    getline(cin,s); 
      while(n-->0){
         getline(cin,s);
         int d1,d2,y1,y2,m1,m2;
         y1=s[3]-'0'+(s[2]-'0')*10+(s[1]-'0')*100+(s[0]-'0')*1000;
         m1=s[6]-'0'+(s[5]-'0')*10;
         d1=s[9]-'0'+(s[8]-'0')*10;
         y2=s[14]-'0'+(s[13]-'0')*10+(s[12]-'0')*100+(s[11]-'0')*1000;
         m2=s[17]-'0'+(s[16]-'0')*10;
         d2=s[20]-'0'+(s[19]-'0')*10;
         if((m1==1 && d1==1) || (m1==5 && d1<=3) || (m1==10 && d1<=7) || weekday(y1,m1,d1)==6 || weekday(y1,m1,d1)==7){
                cout<<buf[y2][m2][d2]-buf[y1][m1][d1]<<endl;
         }
         else{
             cout<<buf[y2][m2][d2]-buf[y1][m1][d1]+1<<endl;
         }
     }
     return 0;
} 

 




这是我的代码,觉得没有错,数据也是对的,但提交就是runtime error,很迷。

附上ac的代码:

    #include <iostream>  
    #include <stdio.h>  
    using namespace std;  
    int dayOfMonth[][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};  
    struct Date  
    {  
        int y,m,d;  
        int leap()  
        {  
            return (y%4==0&&y%100!=0||y%400==0?1:0);  
        }  
        int week()  
        {  
            int Y=y,M=m;  
            if(m==1||m==2)  
            {  
                Y--;  
                M+=12;  
            }  
            int W=(d+M*2+3*(M+1)/5+Y+Y/4-Y/100+Y/400)%7;  
            return W+1;  
        }  
        Date(){}  
        Date(int yy,int mm,int dd){y=yy;m=mm;d=dd;}  
        Date getNext()  
        {  
            Date t(y,m,d);  
            if(d<dayOfMonth[leap()][m])  
                t.d+=1;  
            else if(m<12)  
            {  
                t.m+=1;  
                t.d=1;  
            }  
            else  
            {  
                t.y+=1;  
                t.m=1;  
                t.d=1;  
            }  
            return t;     
        }  
          
        bool dealDay()  
        {  
            if(week()==6||week()==7)  
                return false;  
            if(m==1&&d==1||m==5&&d>=1&&d<=3||m==10&&d>=1&&d<=7)  
                return false;  
            return true;  
        }  
        bool isWeekend()  
        {  
            return week()==6||week()==7;  
        }  
          
    };  
    bool equals(Date d1,Date d2)  
    {  
            return d1.y==d2.y&&d1.m==d2.m&&d1.d==d2.d;  
    }  
      
    int ansOfYear(int year)  
    {  
        bool isleap=(year%4==0&&year%100!=0||year%400==0);     
        int res=(isleap?366:365),t1,t2,t3=0,i;  
        t1=104;  
        if(isleap)  
        {  
            if(Date(year,12,30).isWeekend())  
                t1+=1;  
            if(Date(year,12,31).isWeekend())  
                t1+=1;  
        }  
        else  
        {  
            if(Date(year,12,31).isWeekend())  
                t1+=1;  
        }  
        t2=11;  
        if(Date(year,1,1).isWeekend())  
                t3+=1;  
        for(i=1;i<4;i++)  
            if(Date(year,5,i).isWeekend())  
                t3+=1;  
        for(i=1;i<8;i++)  
            if(Date(year,10,i).isWeekend())  
                t3+=1;  
        res=res-t1-t2+t3;  
        return res;  
    }  
      
    int main()  
    {  
        //freopen("input.txt","r",stdin);
        int n,res=0,i;  
        Date d1,d2,t;  
        scanf("%d",&n);  
        while(n--)  
        {  
            res=0;  
            scanf("%d-%d-%d %d-%d-%d",&d1.y,&d1.m,&d1.d,&d2.y,&d2.m,&d2.d);  
            if(d2.y-d1.y<=1)  
            {  
                for(t=d1;!equals(t,d2.getNext());t=t.getNext())  
                    if(t.dealDay())  
                        res+=1;  
            }  
            else      
            {  
                for(t=d1;!equals(t,Date(d1.y+1,1,1));t=t.getNext())  
                    if(t.dealDay())  
                        res+=1;  
                for(t=Date(d2.y,1,1);!equals(t,d2.getNext());t=t.getNext())  
                    if(t.dealDay())  
                        res+=1;  
                for(i=d1.y+1;i<d2.y;i++)  
                    res+=ansOfYear(i);  
            }  
            printf("%d\n",res);  
        }  
        return 0;  
    }

 




转载于:https://www.cnblogs.com/bernieloveslife/p/7825349.html

二哥的Java进阶之路可以说是充满挑战的,但也充满了机遇和成长。首先,为了更好地提升自己的技术能力,二哥选择了系统地学习Java编程语言。他通过参加专业的培训课程和自主学习,深入了解了Java的基础知识,掌握了常用的语法和编码规范。 接下来,二哥积极参与了一些项目,这让他在实践中不断掌握和运用Java的各种技术。在项目中,他遇到了许多技术难题和挑战,但正是这些挑战让他有机会不断提升自己的能力和解决问题的能力。通过不断地思考、学习和实践,他逐渐掌握了JavaWeb开发、数据库设计与优化等相关技术。 除了项目经验,二哥还积极参加技术交流与分享活动,这不仅帮助他与其他开发者建立了联系,还提供了更多学习和思考的机会。他参加了一些技术大会、社区活动和技术论坛,结识了一批优秀的技术人才,听取了他们的经验分享,学习了他们的思路和方法论。 在进阶的过程中,二哥养成了良好的学习习惯和思考能力。他经常通过阅读相关书籍、博客和技术文档来扩充知识面,不断关注行业的最新发展和趋势。同时,他也用工作之外的时间进行代码的编写和项目的实践,不断提升自己的实际操作能力。 通过这一系列的努力,二哥的Java技术水平得到了提升,他逐渐成为了一名技术过硬、经验丰富的Java开发工程师。在未来的道路上,他将继续保持学习的态度,不断探索和应用新的技术,为自己的职业发展注入无限的动力。他相信,只要不断努力,他的Java进阶之路将会越来越宽广。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值