版权声明:本文为作者原创文章,转载请注明出处。 https://blog.csdn.net/Glasier/article/details/79630724 </div><link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css"><div class="htmledit_views" id="content_views"><div class="pdsec" style="font-family:'微软雅黑', '黑体', '宋体';color:rgb(51,51,51);">问题描述</div><div class="pdcont" style="font-family:'宋体', 'Times New Roman';font-size:14px;line-height:22px;color:rgb(51,51,51);"> 小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。<br><br> 比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。<br><br> 给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?</div><div class="pdsec" style="font-family:'微软雅黑', '黑体', '宋体';color:rgb(51,51,51);">输入格式</div><div class="pdcont" style="font-family:'宋体', 'Times New Roman';font-size:14px;line-height:22px;color:rgb(51,51,51);"> 一个日期,格式是"AA/BB/CC"。 (0 <= A, B, C <= 9)</div><div class="pdsec" style="font-family:'微软雅黑', '黑体', '宋体';color:rgb(51,51,51);">输出格式</div><div class="pdcont" style="font-family:'宋体', 'Times New Roman';font-size:14px;line-height:22px;color:rgb(51,51,51);"> 输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。</div><div class="pdsec" style="font-family:'微软雅黑', '黑体', '宋体';color:rgb(51,51,51);">样例输入</div><div class="pddata" style="font-family:'Courier New', Courier, monospace;font-size:14px;color:rgb(51,51,51);">02/03/04</div><div class="pdsec" style="font-family:'微软雅黑', '黑体', '宋体';color:rgb(51,51,51);">样例输出</div><div class="pddata" style="font-family:'Courier New', Courier, monospace;font-size:14px;color:rgb(51,51,51);">2002-03-04<br style="font-family:'宋体', 'Times New Roman';">2004-02-03<br style="font-family:'宋体', 'Times New Roman';">2004-03-02</div><div class="pddata" style="font-family:'Courier New', Courier, monospace;font-size:14px;color:rgb(51,51,51);"><br></div><p></p><div><strong><span style="color:#000066;">需要判断闰年,<strong><span style="color:#000066;">注意年月日的限制,然后</span></strong>对年月日进行如题三种排序,判断后放到set中,按序输出就可以了。</span></strong></div><div><strong><span style="color:#000066;">代码如下。</span></strong></div><div><br></div><p></p><pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><cstring></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">int</span> m[<span class="hljs-number">2</span>][<span class="hljs-number">13</span>]={ <span class="hljs-number">0</span>,<span class="hljs-number">31</span>,<span class="hljs-number">28</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-number">0</span>,<span class="hljs-number">31</span>,<span class="hljs-number">29</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>,<span class="hljs-number">30</span>,<span class="hljs-number">31</span>};</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in">set</span><<span class="hljs-built_in">string</span>>e;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">check</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a[])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> c[<span class="hljs-number">15</span>];</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">string</span> s;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> y=a[<span class="hljs-number">0</span>]+<span class="hljs-number">1900</span>,f=<span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(y<<span class="hljs-number">1960</span>) y+=<span class="hljs-number">100</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>((y%<span class="hljs-number">4</span>==<span class="hljs-number">0</span>&&y%<span class="hljs-number">100</span>)||y%<span class="hljs-number">400</span>==<span class="hljs-number">0</span>) f=<span class="hljs-number">1</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(<span class="hljs-number">1</span><=a[<span class="hljs-number">1</span>]&&a[<span class="hljs-number">1</span>]<=<span class="hljs-number">12</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(<span class="hljs-number">1</span><=a[<span class="hljs-number">2</span>]&&a[<span class="hljs-number">2</span>]<=m[f][a[<span class="hljs-number">1</span>]])</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">sprintf</span>(c,<span class="hljs-string">"%d-%02d-%02d"</span>,y,a[<span class="hljs-number">1</span>],a[<span class="hljs-number">2</span>]);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(e.count(s=c)==<span class="hljs-number">0</span>) e.insert(s);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> a[<span class="hljs-number">3</span>],t;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%2d/%2d/%2d"</span>,&a[<span class="hljs-number">0</span>],&a[<span class="hljs-number">1</span>],&a[<span class="hljs-number">2</span>]);check(a);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> t=a[<span class="hljs-number">2</span>];a[<span class="hljs-number">2</span>]=a[<span class="hljs-number">1</span>];a[<span class="hljs-number">1</span>]=a[<span class="hljs-number">0</span>];a[<span class="hljs-number">0</span>]=t;check(a);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> t=a[<span class="hljs-number">2</span>];a[<span class="hljs-number">2</span>]=a[<span class="hljs-number">1</span>];a[<span class="hljs-number">1</span>]=t;check(a);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">for</span>(<span class="hljs-built_in">set</span><<span class="hljs-built_in">string</span>>::iterator it=e.begin();it!=e.end();it++) <span class="hljs-built_in">cout</span><<*it<<<span class="hljs-built_in">endl</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="35"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制"></div></pre><br><p><br></p><p><br></p> </div></div>