有人能告诉我这™是什么情况吗

P1167 刷题

cyqjyxhn @ 2024-12-07 12:01:50

众所周知,这题细节的比头发丝还细节

但是我还是成功的写出来了

啊对,没错

……

在调代码的时候,我遇到了麻烦

(你也可以不看代码,这边只是展示我的代码)

(仅此而已)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,a[6000]={0};
int year1,month1,day1,hour1,minute1;
int year2,month2,day2,hour2,minute2;
long long sum=0;
int ans=0;
bool is_leap_year(int x){
    if(x%4==0&&x%100!=0)return true;
    if(x%400==0)return true;
    return false;
}
int month_judge(int x,int y){
    switch(x){
        case 1:return 31;
        case 3:return 31;
        case 4:return 30;
        case 5:return 31;
        case 6:return 30;
        case 7:return 31;
        case 8:return 31;
        case 9:return 30;
        case 10:return 31;
        case 11:return 30;
        case 12:return 31;
        default:break;
    }
    if(is_leap_year(y))return 29;
    else return 28;
}
void calculate();
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    scanf("%d-%d-%d-%d:%d",&year1,&month1,&day1,&hour1,&minute1);
    scanf("%d-%d-%d-%d:%d",&year2,&month2,&day2,&hour2,&minute2);
    calculate();
    //printf("%lld\n",sum);
    for(int i=1;i<=n;i++){
        if(sum>=a[i]){
            sum-=a[i];
            ans++;
        }
        else break;
    }
    printf("%d",ans);
    return 0;
} 
void calculate(){
    sum+=(year2-year1)*365*24*60;
    for(int i=year1+1;i<year2;i++)if(is_leap_year(i))sum+=24*60;
    //printf("算年:%lld\n",sum);
    if(year1<year2)for(int i=month1+1;i<=12;i++)sum+=month_judge(i,year1)*24*60;
    if(year1<year2)for(int i=1;i<=month2-1;i++)sum+=month_judge(i,year2)*24*60;
    //printf("算月:%lld\n",sum);
    if(month1<month2)sum+=(month_judge(month1,year1)-day1)*24*60;
    if(month1<month2)sum+=(day2-month_judge(month2,year2))*24*60;
    //printf("算日:%d %d %d %lld\n",month_judge(month1,year1),day1,(month_judge(month1,year1)-day1)*24*60,sum);
    if(day1<day2)sum+=(23-hour1)*60;
    if(day1<day2)sum+=hour2*60;
    //printf("算时:%lld\n",sum);
    if(hour1<hour2)sum+=60-minute1;
    if(hour1<hour2)sum+=minute2;
    else sum+=minute2-minute1;
    //printf("算分:%lld\n",sum);
    return ;
}
/*
1
1
2021-10-26-10:26
2021-11-25-10:26

*/

请注意59-61行

    if(month1<month2)sum+=(month_judge(month1,year1)-day1)*24*60;
    if(month1<month2)sum+=(day2-month_judge(month2,year2))*24*60;
    //printf("算日:%d %d %d %lld\n",month_judge(month1,year1),day1,(month_judge(month1,year1)-day1)*24*60,sum);

如果用:

1
1
2021-10-26-10:26
2021-11-25-10:26

该样例时结果是0

很明显month1是小于month2的

并且你单独运行一下发现他是能加上去的

为什么放在程序里sum没加上去?


by cyqjyxhn @ 2024-12-07 12:08:48

现在的问题是

为什么会算出负数


by cyqjyxhn @ 2024-12-08 09:12:26

不需要了谢谢


by cyqjyxhn @ 2025-01-05 09:21:18

@_chiyu1236


|