[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 shelluserwlb 于 2014-11-17 10:15 编辑

  1. @echo off
  2. goto :start
  3. 程序4
  4. 题目:输入某年某月某日,判断这一天是这一年的第几天?
  5. 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
  6.       情况,闰年且输入月份大于3时需考虑多加一天。
  7. :start
  8. color 9f
  9. setlocal EnableDelayedExpansion
  10. set /a total=0
  11. set /a f=0
  12. set /p ndate=输入年月日(例: 2014.11.10):
  13. ::从输入的信息获取年、月、日
  14. for /f "tokens=1,2,3 delims=." %%i in ("%ndate%") do (
  15.     set /a nyear=%%i
  16.     set /a nmonth=%%j
  17.     set /a nday=%%k
  18.     if "!nmonth:~0,1!" == "0" set nmonth=!nmonth:~1,1!
  19.     if "!nday:~0,1!" == "0" set nday=!nday:~1,1!
  20. )
  21. ::判断该年是平年或闰年
  22. set /a t1=%nyear%%%400
  23. set /a t2=%nyear%%%4
  24. set /a t3=%nyear%%%100
  25. if %t1% equ 0 set /a f=1
  26. if %t2% equ 0 (if %t3% neq 0 set /a f=1)
  27. ::根据所输入的月份计算总天数
  28. if "%nmonth%" == "1" (set /a total=0+%nday% & goto :1 )
  29. if "%nmonth%" == "2" (set /a total=31+%nday% & goto :1 )
  30. if "%nmonth%" == "3" (set /a total=59+%f%+%nday% & goto :1 )
  31. if "%nmonth%" == "4" (set /a total=90+%f%+%nday% & goto :1 )
  32. if "%nmonth%" == "5" (set /a total=120+%f%+%nday% & goto :1 )
  33. if "%nmonth%" == "6" (set /a total=151+%f%+%nday% & goto :1 )
  34. if "%nmonth%" == "7" (set /a total=181+%f%+%nday% & goto :1 )
  35. if "%nmonth%" == "8" (set /a total=212+%f%+%nday% & goto :1 )
  36. if "%nmonth%" == "9" (set /a total=243+%f%+%nday% & goto :1 )
  37. if "%nmonth%" == "10" (set /a total=273+%f%+%nday% & goto :1 )
  38. if "%nmonth%" == "11" (set /a total=304+%f%+%nday% & goto :1 )
  39. if "%nmonth%" == "12" (set /a total=334+%f%+%nday% & goto :1 )
  40. :1
  41. ::输出计算结果
  42. if %total%==0 (
  43.     echo 输入日期信息错误,请重新输入!& pause>nul
  44.     cls & goto :START
  45. ) else (
  46.     echo %nyear%年%nmonth%月%nday%日是该年的第%total%天。
  47. )
  48. endlocal
  49. pause>nul
复制代码

TOP

本帖最后由 shelluserwlb 于 2014-11-17 10:23 编辑
  1. @echo off
  2. goto :start
  3. 程序11
  4. 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
  5.    后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
  6. 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21...
  7. :start
  8. setlocal EnableDelayedExpansion
  9. set /a f1=1
  10. set /a f2=1
  11. set /p nt=请入月数:
  12. echo 第1个月:1对兔子 & if "%nt%" == "1"  goto :1
  13. echo 第2个月:1对兔子 & if "%nt%" == "2"  goto :1
  14. for /l %%i in (3,1,%nt%) do (
  15.     set /a f3=!f1!+!f2!
  16.     set /a f1=!f2!
  17.     set /a f2=!f3!
  18.     echo 第%%i个月:!f3!对兔子  
  19. )
  20. :1
  21. endlocal
  22. echo 统计完毕!& pause>nul
复制代码

TOP

返回列表