http://www.spoj.com/problems/SAMER08E/i tried for hours debugging this but i'm unable to find the error.i wrote the code pretty length but it is very clear.
#include <iostream>
#include <stdio.h>
#define january 1
#define february 2
#define december 12
using namespace std;
struct months
{
int days;
}month_object[12]={
31,28,31,30,31,30,31,31,30,31,30,31
};
struct date{
int date;
int month;
int year;
long int cons;
};
struct ans
{
long int days;
long int cons;
};
bool isLeapYear(int year)
{
if( ( year%4 == 0 && year % 100 !=0 ) || ( year % 400 == 0 ) )
return true;
else
return false;
}
bool absdiff(struct date ob1,struct date ob2)
{
int last_day;
if(ob2.month == february && isLeapYear(ob2.year))
last_day = month_object[february-1].days+1;
else
last_day = month_object[ob2.month-1].days;
ob2.date= ob2.date%last_day;
if(ob1.date-ob2.date ==1)
return true;
else
return false;
}
bool yearcheck(struct date ob1,struct date ob2)
{
if(ob1.year==ob2.year) return true;
else if(ob1.year-1 == ob2.year && ob2.month == december && ob1.month==january)
return true;
else
return false;
}
bool monthcheck(struct date ob1,struct date ob2)
{
if(ob1.month == ob2.month) return true;
else if(ob1.month == (ob2.month+1)%12)
return true;
else
return false;
}
bool daycheck(struct date ob1,struct date ob2)
{
if((ob1.date-1)==ob2.date) return true;
else if(ob1.date < ob2.date && absdiff(ob1,ob2))
return true;
else
return false;
}
struct ans process(struct date obj[],int n)
{
struct ans ob;
ob.days=0;ob.cons=0;
for(int i=1;i<n;i++)
{
if(yearcheck(obj[i],obj[i-1]) && monthcheck(obj[i],obj[i-1]) && daycheck(obj[i],obj[i-1]))
{
ob.days +=1;
ob.cons +=obj[i].cons - obj[i-1].cons;
}
}
return ob;
}
int main()
{
int n;
while(1){
cin>>n;
if(n==0)
break;
struct date obj[n];
for(int i=0;i<n;i++)
cin>>obj[i].date>>obj[i].month>>obj[i].year>>obj[i].cons;
struct ans ob=process(obj,n);
cout<<ob.days<<" "<<ob.cons<<endl;
}
return 0;
}