1251
[Blue Bridge Cup 2015 preliminary round] galaxy bomb
Many man-made floating Planet X "bomb" in the vast space of the galaxy in X, used as a landmark in the universe.
After each bomb explosion can be set for how many days.
For example: Alpha bomb placed January 1, 2015, timed to 15 days, then it exploded in January 16, 2015.
There is a beta bomb, a day of b c month placement, the timing is n days, you calculate the exact date of its explosion.
Input
there is an input a plurality of sets of data, each data input line, each line of input four positive integers a, b, c, n
input to ensure dates between 1000-01-01 2020-01-01, date and valid.
not more than 1000 n
output
Please fill in this date, in the format of yyyy-mm-dd i.e. two four year date February 2. For example: 2015-02-19
Please exactly as written. Other words or symbols can not appear.
Sample input the Copy
, 2015. 1. 1 15
, 2014 1000. 9. 11
sample output from the Copy
2015-01-16
2017-08-05
prompts
the subject has been adapted.
note
- Sort things out with a pen and paper to draw a picture
- Good function to create reusable functions
- Leap years: four years a leap, leap two hundred years, four hundred years leap
algorithm
python v1.0:
- By month treatment
- Creating isLeap (), used to determine leap year
- Create T (), the parameters, m, y to determine the number of days of the month
python v2.0、C++ v1.0:
- Analog timepiece, plus one cycle time, the time complexity of O (n), n depending on
- Note that in C ++ using printf ( "% 02d", d) to control the output
It can be noted that: the code longer, faster speed qwq, the first monthly reduction month, when n increases, the speed of the two algorithms are similar
answer
python v1.0:
def isLeapYear(year):#四年一闰,两百年不闰,四百年闰
if (not year % 100 == 0) and (year % 4 == 0) or (year % 400 == 0):
return True
def T(m, y):
is31Day = [1,3,5,7,8,10,12]
if m in is31Day:
return 31
elif m == 2:
if isLeapYear(y):
return 29
else:
return 28
else:
return 30
def main(data):
y, m, d, n = map(int,data.split(' '))
acc = 0
acc = d + n
while True:
if T(m, y) == 31:
if acc <= 31:
d = acc
break
else:
acc = acc - 31
if m == 12:
y = y + 1
m = 1
else:
m = m + 1
if T(m, y) == 30:
if acc <= 30:
d = acc
break
else:
m = m + 1
acc = acc - 30
if T(m, y) == 29:
if acc <= 29:
d = acc
break
else:
m = m + 1
acc = acc - 29
if T(m, y) == 28:
if acc <= 28:
d = acc
break
else:
m = m + 1
acc = acc - 28
#print(acc)
print('{:}-{:0>2}-{:0>2}'.format(y,m,d))
while True:
main(input())
python v2.0:
def isLeapYear(year):#四年一闰,两百年不闰,四百年闰
if (not year % 100 == 0) and (year % 4 == 0) or (year % 400 == 0):
return True
else:
return False
def main(data):
D = [31,28,31,30,31,30,31,31,30,31,30,31]
y, m, d, n = map(int,data.split(' '))
if isLeapYear(y):
D[1] = 29
for i in range(n):
d = d + 1
if d > D[m-1]:
d = 1
m = m + 1
if m > 12:
m = 1
y = y + 1
if isLeapYear(y):
D[1] = 29
else:
D[1] = 28
print('{:}-{:0>2}-{:0>2}'.format(y,m,d))
while True:
main(input())
c++ v1.0:
#include<iostream>
using namespace std;
int f(int x){
if(x%4==0&&x%100!=0||x%400==0)
return 1;
else return 0;
}
int d[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
int a,b,c,n;
while(cin>>a>>b>>c>>n){
for(int i=1;i<=n;i++){
if(f(a)) d[1]=29;
else d[1]=28;
if(++c>d[b-1]) {c=1;b++;}
if(b>12){
b=1;
++a; }
}
printf("%d-%02d-%02d\n",a,b,c);
}
return 0;
}
/**************************************************************
Problem: 1251
User: yanshanbei
Language: C++
Result: 正确
Time:0 ms
Memory:2084 kb
****************************************************************/