NC15711. 大吉大利
描述
输入描述
第一行输入一个整数T(表示样例个数)
接下来T组样例
每个样例一行,包含一个字符串“yyyy-mm-dd”(1990<=yyyy<=2018)
题目保证测试数据的正确性
输出描述
输出题意要求的天数
示例1
输入:
1 1999-09-09
输出:
5020
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 484K, 提交时间: 2018-04-21 14:10:26
#include<bits/stdc++.h> using namespace std; int md[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; bool leap(int y){ return y%4==0&&y%100!=0||y%400==0; } int main(){ int T; cin>>T; while(T--){ int y,m,d; scanf("%d-%d-%d",&y,&m,&d); int cnt=0; if (m!=4&&d%10!=4&&y%10!=4)cnt++; if (leap(y))md[2]=29; while(y!=2018||m!=4||d!=21){ d++; if (d>md[m])d=1,m++; if (m>12){m=1;y++;if(leap(y))md[2]=29;else md[2]=28;} if (m!=4&&d%10!=4&&y%10!=4)cnt++; } cout<<cnt<<endl; } }
Java(javac 1.8) 解法, 执行用时: 304ms, 内存消耗: 34100K, 提交时间: 2018-04-21 17:47:38
import java.time.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int T = cin.nextInt(); while(cin.hasNext()) { String str = cin.next(); LocalDate time = LocalDate.parse(str); int cnt=0; while(time.toString().compareTo("2018-04-21")!=0) { String tmp = time.toString(); if(!tmp.contains("4")) cnt++; time = time.plusDays(1); } System.out.println(cnt); } cin.close(); } }
Python(2.7.3) 解法, 执行用时: 311ms, 内存消耗: 2912K, 提交时间: 2018-04-21 15:39:37
import datetime T=int(raw_input()) now=datetime.datetime(2018,4,21) for i in range(T): y,m,d=map(int,raw_input().split("-")) date=datetime.datetime(y,m,d) sum=0 while date<=now: y=set(str(date.year)) m=set(str(date.month)) d=set(str(date.day)) l=y|m|d if '4' not in l: sum+=1 date+=datetime.timedelta(days=1) print(sum)
Python3(3.5.2) 解法, 执行用时: 111ms, 内存消耗: 5212K, 提交时间: 2018-04-21 17:43:16
import time import datetime T = int(input()) for i in range(T): a = input('') t = time.strptime(a,"%Y-%m-%d") year,month,day = t[0:3] x = datetime.date(year,month,day) y = datetime.date(2018,4,21) dd = (y-x).days + 1 delta = datetime.timedelta(days=1) cnt = 0 while x <= y: if '4' in str(x.year)+str(x.month)+str(x.day): cnt += 1 x += delta print(dd-cnt)