# Algorithmic Notes 3.4 Subsection - Introduction Simulation - > Date Processing

Posted by kparish on Fri, 04 Feb 2022 18:12:57 +0100

# A Date Difference

### Title Description

There are two dates. Find the number of days between the two dates. If the two dates are consecutive, we specify two days between them.

### input

There are multiple sets of data, each with two rows representing two dates in the form of YYYYMMDD

### output

Each set of data outputs a row, the date difference

20130101
20130105

5

### Submit Code

```#include<stdio.h>
int isprime(int n){
if((n%4==0&&n%100!=0)||n%400==0)
return 1;
else
return 0;
}
int main(){
int num[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}};
int a,b,t;
int y1,m1,d1,y2,m2,d2;
while(scanf("%d %d",&a,&b)!=EOF){
if(a>b){
t=a;
a=b;
b=t;
}
y1=a/10000;m1=a%10000/100;d1=a%100;
y2=b/10000;m2=b%10000/100;d2=b%100;
int count=0;
while(y1!=y2||m1!=m2||d1!=d2){
d1++;
if(d1==num[m1][isprime(y1)]+1){
d1=1;
m1++;
}
if(m1==13){
m1=1;
y1++;
}
count++;
}
printf("%d\n",count+1);
}
return 0;
}

```

# C Print Date

### Title Description

Give the year minute m and the nth day of the year, and calculate what month the nth day is.

### input

The input consists of two integers y (1<=y<=3000) and N (1<=n<=366).

### output

There may be multiple sets of test data. For each set of data, print the corresponding date in the input in yyyy-mm-dd format.

2013 60
2012 300
2011 350
2000 211

2013-03-01
2012-10-26
2011-12-16
2000-07-29

### Code Submission

```#include <stdio.h>
#include <string.h>

int isprime(int n){
if((n%4==0&&n%100!=0)||n%400==0)
return 1;
else
return 0;
}
int main(){
int month[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}};
int y, m=1, d=0, n;
while(scanf("%d%d", &y, &n)!=EOF){
while(n>0){
d++;
if(d==month[m][isprime(y)]+1){
m++;
d=1;
}
if(m==13){
y++;
m=1;
}
n--;
}
printf("%04d-%02d-%02d\n", y, m, d);
m=1, d=0;
}
return 0;
}

```

# D Date Class

### Title Description

Write a date class that requires the date to be output in XX xx-xx-xx format for an extra day operation.

### input

Enter the first row to represent the number of test cases m, and the next M rows to have three integers separated by spaces representing the year, month and day. Test data will not have leap years.

### output

Output m lines. Output in XX xx-xx-xx format, representing the day after the input date.

2
1999 10 20
2001 1 31

1999-10-21
2001-02-01

### Tips

Note that there must be 0 before the digit date.

### Code Submission

```#include <stdio.h>
#include <string.h>

int isprime(int n){
if((n%4==0&&n%100!=0)||n%400==0)
return 1;
else
return 0;
}

int main()
{
int num[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}};
int year,month,day,days,m;
while(scanf("%d",&m)!=EOF)
{
for(int i=0;i<m;i++)
{
scanf("%d%d%d", &year,&month,&day);

if(day==num[month][isprime(year)]+1){
month++;
day=1;
}
if(month==13){
year++;
month=1;
}
day=day+1;
printf("%04d-%02d-%02d\n", year,month,day);
}
}
return 0;
}

```

# E-Date Accumulation

### Title Description

Design a program that calculates a date plus what date is after a few days.

### input

Enter the first row to represent the number of samples m, and the next M rows to represent the year, month, day, and the cumulative number of days.

### output

Output m lines, each output in the number of yyyy-m m-dd.

1
2008 2 3 100

2008-05-13

### Code Submission

```#include <stdio.h>
#include <string.h>

int isprime(int n){
if((n%4==0&&n%100!=0)||n%400==0)
return 1;
else
return 0;
}

int main()
{
int num[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}};
int day,year,month,cnt;
int n;
scanf("%d",&n);

for(int j = 0;j < n;j++){
scanf("%d %d %d %d",&year,&month,&day,&cnt);
for(int i = cnt;i > 0;i--){
day++;
if(day==num[month][isprime(year)]+1){
month++;
day = 1;
}
if(month == 13){
year++;
month = 1;
}
}
printf("%04d-%02d-%02d\n",year,month,day);
}
return 0;
}

```

Topics: Algorithm Graph Theory