- 论坛徽章:
- 0
|
i need help
...
it's quite easy. you may write a subroutine that verifies:
1) if modulo 4 gives 0, it's a leap year
2) but if modulo 100 also gives 0, then it isn't
3) however, if modelo 400 gives 0 again, then it's a leap year again.
but i'd like not do so complicated. Much easier will be:
- #!/bin/sh
- test -z $1 && echo "Usage: $0 YYYY" && exit 1
- YYYY=$1
- STR=`cal 02 $YYYY`
- LASTDAY=`echo $STR | awk '{print $NF}'`
- if [ x$LASTDAY = x28 ]; then
- echo "No, year $YYYY is NOT a leap year."
- elif [ x$LASTDAY = x29 ]; then
- echo "Yes, year $YYYY is a leap year."
- else
- echo "eh... something wrong with year $YYYY..."
- exit 2
- fi
复制代码
but if you are a student majored in computer science and it's your assignment, you'd better do it in ordinary way, since your professor will be happy with your algorithm instead of solution. |
|