【问题标题】:spoj samer08e getting wrong answerspoj samer08e 得到错误的答案
【发布时间】:2014-01-04 15:36:46
【问题描述】:

这是我对 spoj 问题的解决方案:

http://www.spoj.com/problems/SAMER08E/

这是我的逻辑:

我检查给定的日期是否彼此相邻。如果是,则添加成本差异。否则跳过。

//header files omitted

#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,st,end) for(int i=st;i<end;i++)

int monthDates[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

bool isLeap( int y ){//check if year is leap
 if(y%4 == 0 && y%100 != 0 || y%400 == 0)
     return true;
return false;
}

bool checkAdjacent( int prev[], int curr[] ){//check if the dates are adjacent to each other
if( prev[0] == 31 && prev[1] == 12 && curr[0] == 1 && curr[1] == 1 && (curr[2] - prev[2]) == 1 )//last and first days of the year
    return true;
if( prev[2] == curr[2]){//same year
    if( prev[1] == curr[1]){//same month
        if((curr[0]-prev[0]) == 1)//same adjacent dates
            return true;
    }else if( curr[1] - prev[1] == 1){//adjacent months
        if( isLeap(prev[2]) && prev[1] == 2 && curr[1] == 3 && prev[0] == 29 && curr[0] == 1)//for leap year february
            return true;
        else if( !isLeap(prev[2]) && monthDates[prev[1]] == prev[0] && curr[0] == 1)
            return true;
    }
}
return false;
}


int main(){
int n;
while( scanf("%d", &n) && n){
    int prev[4], curr[4], count = 0;
    ll totalCost = 0;
    REP(i,4)
        scanf("%d", &prev[i]);
    FOR( i, 1, n){
        REP(j,4)
            scanf("%d", &curr[j]);
        if( checkAdjacent( prev, curr) ){
            totalCost += curr[3] - prev[3];
            count++;
        }
        prev[0] = curr[0];
        prev[1] = curr[1];
        prev[2] = curr[2];
        prev[3] = curr[3];
    }
    printf("%d %lld\n", count, totalCost);
}
return 0;
}

该程序适用于测试用例,但我一直得到错误的答案。错误是什么?

【问题讨论】:

  • 请包括所有代码,包括宏。
  • 在不带括号的情况下混合使用 ands 和 ors(就像你在 isLeap 中所做的那样)通常不是一个好主意。
  • 我检查了 isLeap 函数。它工作正常。

标签: c++ algorithm


【解决方案1】:

由于有两个人记录测量结果,同一天可能会记录两次,这可能会影响您的消费计算。

【讨论】:

  • “有时候 Martin 会这样做,有时候 Isa 会。”
  • 不明确排除双方都这样做的日子。
  • 在输入中指定它们将严格按递增顺序排列,并且没有两个日期相同。
  • 它说没有两个 CONSUMPTIONS 是相同的;它没有说明相邻日期。
【解决方案2】:

当年份是闰年且月份不是二月时,您的程序不会检查条件。添加此条件,您将获得 AC。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多