写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文!
本博客全网唯一合法URL:http://www.cnblogs.com/acm-icpcer/p/8779376.html
前一日函数PreDate是NextDate的逆函数,即给定一个年份、月份、日期,会返回前一天的日期。
输入要求:
年的取值在 1812 年到 2017 年。
日的取值在 1 日到 31 日之间。
月的取值在 1 到 12 月之间
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; int main() { int year,month,day; cout<<"input NextDate(year month day):"<<endl; while(scanf("%d %d %d",&year,&month,&day)==3&&year&&month&&day) { if((year<1812||year>2017)||(month<1||month>12)||(day<1||day>31)) { cout<<"input ERROR detected!"<<endl; continue; } bool is_leapyear=false; int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if((year%100!=0)&&(year%400==0||year%4==0)) { is_leapyear=true; days[2]++; } if(day>days[month]) { cout<<"input ERROR detected!"<<endl; continue; } day--; if(day<=0) { day=days[month-1]; if(day==0) day=31;//in case of 1->12 month--; } if(month<=0) { month=12; year--; } cout<<year<<'/'<<month<<'/'<<day<<endl; } return 0; }
决策表:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
C1:月份在 | M1 | M1 | M1 | M2 | M2 | M3 | M3 | M4 | M4 |
C2:日期在 | D1 | D2-D5 | D6 | D1 | D2-D6 | D1 | D2-D6 | D1 | D2-D3 |
C2:年份在 |
|
|
|
|
|
|
|
|
|
a1:不可能 |
|
| X |
|
|
|
|
|
|
a2:day减1 |
| X |
|
| X |
| X |
| X |
a3:day复位31 | X |
|
|
|
| X |
| X |
|
a4:day复位30 |
|
|
| X |
|
|
|
|
|
a5:day复位29 |
|
|
|
|
|
|
|
|
|
a6:day复位28 |
|
|
|
|
|
|
|
|
|
a7:月减1 | X |
|
| X |
|
|
| X |
|
a8:月复位12 |
|
|
|
|
| X |
|
|
|
a9:年减一 |
|
|
|
|
| X |
|
|
|
| 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
C1:月份在 | M4 | M4 | M4 | M5 | M5 | M5 | M6 | M6 |
C2:日期在 | D4 | D4 | D5-D6 | D1 | D1 | D2-D6 | D1 | D2-D6 |
C2:年份在 | Y1 | Y2 |
| Y1 | Y2 |
|
|
|
a1:不可能 |
| X | X |
|
|
|
|
|
a2:day减1 | X |
|
|
|
| X |
| X |
a3:day复位31 |
|
|
|
|
|
| X |
|
a4:day复位30 |
|
|
|
|
|
|
|
|
a5:day复位29 |
|
|
| X |
|
|
|
|
a6:day复位28 |
|
|
|
| X |
|
|
|
a7:月减1 |
|
|
| X | X |
| X |
|
a8:月复位12 |
|
|
|
|
|
|
|
|
a9:年减一 |
|
|
|
|
|
|
|
|
表格说明:
M1={月份:4,6,9,11月}
M2={月份:5,7,10,12月}
M3={月份:1月}
M4={月份:2月}
M5={月份:3月}
M6={月份:8月}
D1={日期:1日}
D2={日期:2<=日期<=27}
D3={日期:28日}
D4={日期:29日}
D5={日期:30日}
D6={日期:31日}
Y1={年:是闰年}
Y2={年:不是闰年}
测试结果:
用例ID | 输入数据 | 预期输出 | 实际输出 | 是否通过 | 备注 |
001 | 2015/4/1 | 2015/3/31 | 2015/3/31 | Yes |
|
002 | 2015/4/5 | 2015/4/4 | 2015/4/4 | Yes |
|
003 | 2015/6/31 | Error | Error | Yes |
|
004 | 2015/7/1 | 2015/6/30 | 2015/6/30 | Yes |
|
005 | 2015/7/4 | 2015/7/3 | 2015/7/3 | Yes |
|
006 | 2015/1/1 | 2014/12/31 | 2014/12/31 | Yes |
|
007 | 2015/1/2 | 2015/1/1 | 2015/1/1 | Yes |
|
008 | 2015/2/1 | 2015/1/31 | 2015/1/31 | Yes |
|
009 | 2015/2/28 | 2015/2/27 | 2015/2/27 | Yes |
|
010 | 2016/2/29 | 2016/2/28 | 2016/2/28 | Yes |
|
011 | 2015/2/29 | Error | Error | Yes |
|
012 | 2016/2/31 | Error | Error | Yes |
|
013 | 2016/3/1 | 2016/2/29 | 2016/2/29 | Yes |
|
015 | 2015/3/25 | 2015/3/24 | 2015/3/24 | Yes |
|
016 | 2015/8/1 | 2015/7/31 | 2015/7/31 | Yes |
|
017 | 2015/8/5 | 2015/8/4 | 2015/8/4 | Yes |
|
画这个决策表,我的想法是年月日三项分别代表一个自变量,每个自变量都有自己的中断点,这个些中断点可以通过分类讨论的方法来进行等价类划分。比如,年在此分为闰年和非闰年,月可分为有31日的月份、有30日的月份和2月就足够了(但是实际上我进行了进一步的细化)。日分为30、31、28、29四类即可。然后根据这些划分了2*3*4=24个等价类,将触发了相同结果的等价类合并为一以减少测试用例的数量。代码没有什么可以说的,纯面向过程,只要根据最终的测试结果完善完善即可。
tz@COI HZAU
2018/4/10