刷题笔记|计算两个年份间某一日期是周末的天数,发现ctime库对象有时间范围(C++|Python )

简介: 刷题笔记|计算两个年份间某一日期是周末的天数,发现ctime库对象有时间范围(C++|Python )

8cb0b033d88bd9aed88021da1ff1d040.png


——题目来源于问答频道的网友提问



C++版本

#include <iostream>
using namespace std;
int daysinyear(int year)
{
  return 365 + ((year%4==0 && year%100!=0) || year%400==0);
}
int count(int year1,int year2)
{
  int t, total = 0;
  int weekday[3000-1900+1];
  weekday[0] = 7;
  for (int i=1901;i<=3000;i++){
    t = daysinyear(i) + weekday[i-1900 - 1] - 1;
    weekday[i-1900] = t % 7 + 1;
  } 
  for (int i=year1;i<=year2;i++){
    if (weekday[i-1900]==6 || weekday[i-1900]==7){
      total++;
    }
  }
  return total;
}
int main ()
{
  int y1,y2;
  cin >> y1 >> y2;
  cout << count(y1,y2) << endl;
    return 0;
}



稍作修改,可以输出所有满足条件的年份:

#include <iostream>
using namespace std;
int daysinyear(int year)
{
  return 365 + ((year%4==0 && year%100!=0) || year%400==0);
}
void count(int year1,int year2)
{
  int t, total=0;
  int weekday[3000-1900+1];
  int years[3000-1900+1];
  weekday[0] = 7;
  for (int i=1901;i<=3000;i++){
    t = daysinyear(i) + weekday[i-1900 - 1] - 1;
    weekday[i-1900] = t % 7 + 1;
  }
  t = 0;
  for (int i=year1;i<=year2;i++){
    if (weekday[i-1900]==6 || weekday[i-1900]==7){
      total++;
      years[t++] = i;
    }
  }
  cout << year1 << "年到" << year2 << "年间符合要求的年份共有" ;
  cout << total << "个:" << endl; 
  for (int i=0;i<total;i++){
    cout << years[i] << " ";
    if (i%10==9) cout << endl;
  }
  cout << endl;
}
int main ()
{
  count(1900,3000);
  count(2018,2100);
    return 0;
}


输出内容:  


1900年到3000年间符合要求的年份共有314个:


1900 1905 1906 1911 1916 1917 1922 1923 1928 1933

1934 1939 1944 1945 1950 1951 1956 1961 1962 1967

1972 1973 1978 1979 1984 1989 1990 1995 2000 2001

2006 2007 2012 2017 2018 2023 2028 2029 2034 2035

2040 2045 2046 2051 2056 2057 2062 2063 2068 2073

2074 2079 2084 2085 2090 2091 2096 2102 2103 2108

2113 2114 2119 2124 2125 2130 2131 2136 2141 2142

2147 2152 2153 2158 2159 2164 2169 2170 2175 2180

2181 2186 2187 2192 2197 2198 2204 2209 2210 2215

2220 2221 2226 2227 2232 2237 2238 2243 2248 2249

2254 2255 2260 2265 2266 2271 2276 2277 2282 2283

2288 2293 2294 2299 2300 2305 2306 2311 2316 2317

2322 2323 2328 2333 2334 2339 2344 2345 2350 2351

2356 2361 2362 2367 2372 2373 2378 2379 2384 2389

2390 2395 2400 2401 2406 2407 2412 2417 2418 2423

2428 2429 2434 2435 2440 2445 2446 2451 2456 2457

2462 2463 2468 2473 2474 2479 2484 2485 2490 2491

2496 2502 2503 2508 2513 2514 2519 2524 2525 2530

2531 2536 2541 2542 2547 2552 2553 2558 2559 2564

2569 2570 2575 2580 2581 2586 2587 2592 2597 2598

2604 2609 2610 2615 2620 2621 2626 2627 2632 2637

2638 2643 2648 2649 2654 2655 2660 2665 2666 2671

2676 2677 2682 2683 2688 2693 2694 2699 2700 2705

2706 2711 2716 2717 2722 2723 2728 2733 2734 2739

2744 2745 2750 2751 2756 2761 2762 2767 2772 2773

2778 2779 2784 2789 2790 2795 2800 2801 2806 2807

2812 2817 2818 2823 2828 2829 2834 2835 2840 2845

2846 2851 2856 2857 2862 2863 2868 2873 2874 2879

2884 2885 2890 2891 2896 2902 2903 2908 2913 2914

2919 2924 2925 2930 2931 2936 2941 2942 2947 2952

2953 2958 2959 2964 2969 2970 2975 2980 2981 2986

2987 2992 2997 2998



2018年到2100年间符合要求的年份共有23个:

2018 2023 2028 2029 2034 2035 2040 2045 2046 2051

2056 2057 2062 2063 2068 2073 2074 2079 2084 2085

2090 2091 2096

--------------------------------


Process exited after 1.623 seconds with return value 0

请按任意键继续. . .

原本想用<ctime>库来验证:

#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
  time_t ltm;
  struct tm *info;
  time(&ltm); 
    info = localtime(&ltm);
  for (int year = 1970; year<2038; year++){
      info->tm_year = year - 1900;
      info->tm_mon  = 11 - 1;
      info->tm_mday = 11;
      mktime(info); 
      if ((info->tm_wday)==0 || (info->tm_wday)==6)
        cout << year <<endl;    
   }
    return 0;
}
/*
1972
1973
1978
1979
1984
1989
1990
1995
2000
2001
2006
2007
2012
2017
2018
2023
2028
2029
2034
2035
*/



后来发现只能正确输出这一段年份,百度了一下,原来它的CTime对象是有指定范围的:


   static CTime WINAPI GetCurrentTime( );

   获取系统当前日期和时间。返回表示当前日期和时间的CTime对象。

   int GetYear( ) const;

   获取CTime对象表示时间的年份。范围从1970年1月1日到2038年(包括2038年)1月18日。




python版本


python的日期时间库没有限制:

import datetime as dt
years = []
for i in range(1900,3001):
    if dt.date(i,11,11).weekday() in [5,6]:
        years.append(i)
m,n = map(int,input('请输入两个年份(1900~3000)').split())
total = list(filter(lambda x:m<=x<=n, years))
print(len(total),'\n满足条件的所有年份:\n',years)


还是python简洁,几行代码就能解决问题。

目录
相关文章
|
6天前
|
存储 计算机视觉 C++
在C++中实现Armadillo库与OpenCV库之间的数据格式转换
在C++中实现Armadillo库与OpenCV库之间的数据格式转换是一项常见且实用的技能。上述步骤提供了一种标准的方法来进行这种转换,可以帮助开发者在两个库之间高效地转移和处理数据。虽然转换过程相对直接,但开发者应留意数据类型匹配和性能优化等关键细节。
23 11
|
4天前
|
SQL 前端开发 数据可视化
Rodeo支持多种Python库
Rodeo支持多种Python库
12 5
|
2天前
|
数据采集 存储 JSON
从零到一构建网络爬虫帝国:HTTP协议+Python requests库深度解析
在网络数据的海洋中,网络爬虫遵循HTTP协议,穿梭于互联网各处,收集宝贵信息。本文将从零开始,使用Python的requests库,深入解析HTTP协议,助你构建自己的网络爬虫帝国。首先介绍HTTP协议基础,包括请求与响应结构;然后详细介绍requests库的安装与使用,演示如何发送GET和POST请求并处理响应;最后概述爬虫构建流程及挑战,帮助你逐步掌握核心技术,畅游数据海洋。
17 3
|
8天前
|
数据采集 网络协议 API
HTTP协议大揭秘!Python requests库实战,让网络请求变得简单高效
【9月更文挑战第13天】在数字化时代,互联网成为信息传输的核心平台,HTTP协议作为基石,定义了客户端与服务器间的数据传输规则。直接处理HTTP请求复杂繁琐,但Python的`requests`库提供了一个简洁强大的接口,简化了这一过程。HTTP协议采用请求与响应模式,无状态且结构化设计,使其能灵活处理各种数据交换。
36 8
|
6天前
|
存储 计算机视觉 C++
在C++中实现Armadillo库与OpenCV库之间的数据格式转换
在C++中实现Armadillo库与OpenCV库之间的数据格式转换是一项常见且实用的技能。上述步骤提供了一种标准的方法来进行这种转换,可以帮助开发者在两个库之间高效地转移和处理数据。虽然转换过程相对直接,但开发者应留意数据类型匹配和性能优化等关键细节。
14 3
|
12天前
|
JSON API 开发者
Python网络编程新纪元:urllib与requests库,让你的HTTP请求无所不能
【9月更文挑战第9天】随着互联网的发展,网络编程成为现代软件开发的关键部分。Python凭借简洁、易读及强大的特性,在该领域展现出独特魅力。本文介绍了Python标准库中的`urllib`和第三方库`requests`在处理HTTP请求方面的优势。`urllib`虽API底层但功能全面,适用于深入控制HTTP请求;而`requests`则以简洁的API和人性化设计著称,使HTTP请求变得简单高效。两者互补共存,共同推动Python网络编程进入全新纪元,无论初学者还是资深开发者都能从中受益。
30 7
|
18天前
|
机器学习/深度学习 PyTorch 算法框架/工具
python这些库和框架哪个更好
【9月更文挑战第2天】python这些库和框架哪个更好
32 6
|
18天前
|
机器学习/深度学习 数据采集 算法框架/工具
python有哪些常用的库和框架
【9月更文挑战第2天】python有哪些常用的库和框架
21 6
|
18天前
|
C++
fedora安装静态c和c++库
fedora安装静态c和c++库
WK
|
19天前
|
数据采集 XML 安全
常用的Python网络爬虫库有哪些?
Python网络爬虫库种类丰富,各具特色。`requests` 和 `urllib` 简化了 HTTP 请求,`urllib3` 提供了线程安全的连接池,`httplib2` 则具备全面的客户端接口。异步库 `aiohttp` 可大幅提升数据抓取效率。
WK
36 1