栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1033 Ambiguous Dates

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1033 Ambiguous Dates

#include <iostream>  #include <string.h>  #include <vector>  #include <math.h>  #include <stdio.h>  #include <set>;  #include <map>  #include <algorithm>  using namespace std;  int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  set<int> s;  bool IsLeap(int y)  {      return (y % 100 == 0 && y % 400 == 0) || (y % 100 != 0 && y % 4 == 0);  }  int GetDaysInNonth(int y, int m)  {      return (m == 2 && IsLeap(y)) ? days[m-1] + 1 : days[m-1];  }  int RelativeTo(int y, int m, int d)  {      int sum = 0;      for (int i = 1700; i < y; i++)          for (int j = 0; j < 12; j++)   sum += GetDaysInNonth(i, j+1);      for (int i = 1; i < m; i++)          sum += GetDaysInNonth(y, i);      sum = sum + d - 1;      return sum - 110245;  }  int String2Int(char str[])  {      int sum = 0;      for (int i = 0; i < strlen(str); i++)          sum = 10 * sum + str[i] - '0';      return sum;  }  bool IsValid(int y, int m, int d)  {      return y >= 1700 && y <= 2299 && m >= 1 && m <= 12 && d >= 1 && d <= GetDaysInNonth(y, m);  }  bool ContainDelimiter(char str[])  {      for (int i = 0; i < strlen(str); i++)          if (str[i] < '0' || str[i] > '9')   return true;     return false;  }  void Insert(char y[], char m[], char d[])  {      if (strlen(m) <= 2 && strlen(d) <= 2)      {          if (strlen(y) <= 2)          {   for (int i = 1700; i <= 2200; i += 100)       if (IsValid(i + String2Int(y), String2Int(m), String2Int(d)))s.insert(RelativeTo(i + String2Int(y), String2Int(m), String2Int(d)));          }         else if (strlen(y) == 4)          {   if (IsValid(String2Int(y), String2Int(m), String2Int(d)))       s.insert(RelativeTo(String2Int(y), String2Int(m), String2Int(d)));          }     }  }  void ValidateAll(char s0[], char s1[], char s2[])  {      Insert(s0, s1, s2);      Insert(s0, s2, s1);      Insert(s1, s0, s2);      Insert(s1, s2, s0);      Insert(s2, s0, s1);      Insert(s2, s1, s0);  }  int main()  {     int t;      char line[15];      char s0[15], s1[15], s2[15];      cin >> t;      for (int i = 0; i < t; i++)      {          cin >> line;         s.clear();          printf("Scenario #%d:n", i+1);          if (ContainDelimiter(line))         {   int j, index = 0;  for (j = 0; line[j] >= '0' && line[j] <= '9'; j++)      s0[index++] = line[j];   s0[index] = '';   index = 0;  for (j++; line[j] >= '0' && line[j] <= '9'; j++)       s1[index++] = line[j];   s1[index] = '';  index = 0;  for (j++; j < strlen(line); j++)      s2[index++] = line[j];   s2[index] = '';   ValidateAll(s0, s1, s2);          }          else          {   for (int j = 0; j < strlen(line)-2; j++)      for (int k = j+1; k < strlen(line)-1; k++)       {         s0[0] = '', s1[0] = '', s2[0] = '';          strncpy(s0, line, j+1);s0[j+1] = '';strncpy(s1, line+j+1, k-j);s1[k-j] = '';strncpy(s2, line+k+1, strlen(line)-k+1);s2[strlen(line)-k+1] = '';ValidateAll(s0, s1, s2);       }          }          if (s.size() == 0)   cout << "Illegal date" << endl;          else   for (set<int>::iterator iter = s.begin(); iter != s.end(); iter++)       cout << *iter << endl;          cout << endl;      }  }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378060.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号