一直接从19600101-20591231枚举
然后再判断日期是否合法 计算量10^7不会超时
#include<bits/stdc++.h>
using namespace std;
int mon_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
bool isyear(int year){
if(year%400==0||year%100!=0&&year%4==0)
return true;
return false;
}
int year,mon,day;
bool judge(int date){
year=date/10000;
mon=date%10000/100;
day=date%100;
if(day<1)
return false;
if(mon<1||mon>12)
return false;
if(mon!=2){
if(day>mon_day[mon]){
return false;
}
}
else{
if(isyear(year)){
if(day>mon_day[2]+1)
return false;
}
else if(day>mon_day[2])
return false;
}
return true;
}
int main()
{
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
for(int i=19600101;i<=20591231;i++){
if(judge(i)){
//年/月/日的,有采用月/日/年的,还有采用日/月/年
if((year%100==a&&mon==b&&day==c)||(mon==a&&day==b&&year%100==c)||(day==a&&mon==b&&year%100==c)){
printf("%d-%02d-%02d\n",year,mon,day);
}
}
}
return 0;
}
二、 完整模拟
自己写的 代码复杂度太高 太不容易。。。
#include<bits/stdc++.h>
using namespace std;
int mon[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
bool isyear(int y) {
if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
return true;
else return false;
}
struct node {
int year;
int mon;
int day;
node() {
year = 0;
mon = 0;
day = 0;
}
node(int y, int m, int d) {
year = y;
mon = m;
day = d;
}
}date[200];
bool operator<(node a, node b) {
if (a.year != b.year) {
return a.year < b.year;
}
else {
if (a.mon != b.mon) {
return a.mon < b.mon;
}
else return a.day < b.day;
}
}
int cnt = 0;
int main()
{
string s;
cin >> s;
int num1 = (s[0] - '0') * 10 + s[1] - '0';
int num2 = (s[3] - '0') * 10 + s[4] - '0';
int num3 = (s[6] - '0') * 10 + s[7] - '0';
int year, month, day;
for (int i = 0; i < 3; i++) {
if (i == 0) {//年月日
year = 1900 + num1;
if (year >= 1960 && year <= 2059) {
if (num2 >= 1 && num2 <= 12) {
if (num2 == 2) {
if (isyear(year)) {
if (num3 >= 1 && num3 <= mon[num2] + 1)
date[cnt++] = node(year, num2, num3);
}
else if (num3 >= 1 && num3 <= mon[num2])
date[cnt++] = node(year, num2, num3);
}
else if (num3 >= 1 && num3 <= mon[num2]) {
date[cnt++] = node(year, num2, num3);
}
}
}
year = 2000 + num1;
if (year >= 1960 && year <= 2059) {
if (num2 >= 1 && num2 <= 12) {
if (num2 == 2) {
if (isyear(year)) {
if (num3 >= 1 && num3 <= mon[num2] + 1)
date[cnt++] = node(year, num2, num3);
}
else if (num3 >= 1 && num3 <= mon[num2])
date[cnt++] = node(year, num2, num3);
}
else if (num3 >= 1 && num3 <= mon[num2]) {
date[cnt++] = node(year, num2, num3);
}
}
}
}
else if (i == 1) {//月 日 年
year = 1900 + num3;
if (year >= 1960 && year <= 2059) {
if (num1 >= 1 && num1 <= 12) {
if (num1 == 2) {
if (isyear(year)) {
if (num2 >= 1 && num2 <= mon[num1] + 1)
date[cnt++] = node(year, num1, num2);
}
else if (num2 >= 1 && num2 <= mon[num1])
date[cnt++] = node(year, num1, num2);
}
else if (num2 >= 1 && num2 <= mon[num1]) {
date[cnt++] = node(year, num1, num2);
}
}
}
year = 2000 + num3;
if (year >= 1960 && year <= 2059) {
if (num1 >= 1 && num1 <= 12) {
if (num1 == 2) {
if (isyear(year)) {
if (num2 >= 1 && num2 <= mon[num1] + 1)
date[cnt++] = node(year, num1, num2);
}
else if (num2 >= 1 && num2 <= mon[num1])
date[cnt++] = node(year, num1, num2);
}
else if (num2 >= 1 && num2 <= mon[num1]) {
date[cnt++] = node(year, num1, num2);
}
}
}
}
else if (i == 2) {//日 月 年
year = 1900 + num3;
if (year >= 1960 && year <= 2059) {
if (num2 >= 1 && num2 <= 12) {
if (num2 == 2) {
if (isyear(year)) {
if (num1 >= 1 && num1 <= mon[num2] + 1)
date[cnt++] = node(year, num2, num1);
}
else if (num1 >= 1 && num1 <= mon[num2])
date[cnt++] = node(year, num2, num1);
}
else if (num1 >= 1 && num1 <= mon[num2]) {
date[cnt++] = node(year, num2, num1);
}
}
}
year = 2000 + num3;
if (year >= 1960 && year <= 2059) {
if (num2 >= 1 && num2 <= 12) {
if (num2 == 2) {
if (isyear(year)) {
if (num1 >= 1 && num1 <= mon[num2] + 1)
date[cnt++] = node(year, num2, num1);
}
else if (num1 >= 1 && num1 <= mon[num2])
date[cnt++] = node(year, num2, num1);
}
else if (num1 >= 1 && num1 <= mon[num2]) {
date[cnt++] = node(year, num2, num1);
}
}
}
}
}
sort(date, date + cnt);
for (int i = 1; i < cnt; i++) {
if (date[i].year == date[i - 1].year && date[i].mon == date[i - 1].mon && date[i].day == date[i - 1].day) {
date[i - 1].year = 0;
}
}
for (int i = 0; i < cnt; i++) {
if (date[i].year == 0)
continue;
cout << date[i].year << '-';
if (date[i].mon < 10)
cout << '0' << date[i].mon << '-';
else cout << date[i].mon << '-';
if (date[i].day < 10)
cout << '0' << date[i].day << endl;
else cout << date[i].day << endl;
//<<date[i].mon<<'-'<<date[i].day<<endl;
}
return 0;
}
枚举举大旗
官方方法用sstream流 这代码量有点绝望啊
直接模拟:
这代码量
这代码量
哈哈 注意逻辑清晰 枚举很长 也是可以AC的
这代码量
这代码量。。。。。
枚举nb