IOI 2026 中国国家集训队作业(试题泛做)个人题解
2025/12/18 22:52:43
GNU date是一个强大的工具,安装后,许多日期计算变得简单。例如判断某一年是否为闰年,只需使用以下代码:
if [ $( date 12/31/$year +%j ) -eq 366 ]如果一年的最后一天是该年的第366天,那么这一年就是闰年。此外,GNU date还能处理较早的日期,而标准Unix的date命令以1970年1月1日00:00:00 UTC作为“时间零点”,无法处理早于这个时间的日期。
脚本dayinpast可以快速帮助我们确定过去某个日期是星期几。以下是脚本代码:
#!/bin/bash # dayinpast--Given a date, reports what day of the week it was if [ $# -ne 3 ] ; then echo "Usage: $(basename $0) mon day year" >&2 echo " with just numerical values (ex: 7 7 1776)" >&2 exit 1 fi date --version > /dev/null 2>&1 # Discard error, if any. baddate="$?"