当前位置:网站首页>【数组】查表法(闰年)
【数组】查表法(闰年)
2022-08-02 14:13:00 【白U】
一维数组 【以空间换时间】
define MONTHERROR -1
#define YEARERROR -2
#define DAYERROR -3
bool Leap_Year(int year)
{
bool res = false;
if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0)
{
res = true;
}
return res;
}
int Get_YM_Day(int year, int month)
{
//空间换时间 static 让其生存期变长,在数据区,const 让其不可修改,能力变小。
//查表法
static const int days[] = {
29,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && Leap_Year(year))
{
month = 0;
}
return days[month];
}
int Get_YMD_Total(int year, int month, int day)
{
int sum = 0;
// int da = 0;
//查表法:static const int sum[] = {0, 0 , 31,59,90,120,151,181,212,243,273,304,334,365};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13
//总天数都是当前的数字加上输入的天数。
if (year < 1) return YEARERROR;
if (month < 1 || month>12) return MONTHERROR;
if (day<1 || day>Get_YM_Day(year, month)) return DAYERROR;
/* if (month>2&& Leap_Year(year,month)) { //如果月份大于2 ,那么才判断是否是闰年,如果小于2就没必要了 da =1; */ }
for (int i = 0; i < month; i++)
{
sum = sum + Get_YM_Day(year, i);
}
return sum + day;
// return sum + day + da ;
}
int main()
{
int year = 0, month = 0, day = 0;
scanf_s("%d/%d/%d", &year, &month, &day);
int n = Get_YMD_Total(year, month, day);
printf("%d/%d/%d=>total: %d\n", year, month, day, n);
// 越界和输入错误判断
switch (n)
{
case YEARERROR:
printf("year input error %d\n", year);
break;
case MONTHERROR:
printf("month input error %d\n",month);
break;
case DAYERROR:
printf("day input error %d\n", day);
break;
default:
printf("%d year %d month %d day\n", year,month,day);
break;
}
return 0;
}
边栏推荐
- 你的站点可能还没有准备好用于Site KitSite Kit 无法访问 WordPress REST API。请确保其已在您的站点上启用。
- LITESTAR 4D应用:室内植物照明模拟
- 剑指offer:反转链表
- Detailed introduction to the hierarchical method of binary tree creation
- 2021-03-12
- C#高级教程
- golang-reflect-method-callback
- 仿真结果的格式&定制
- Run ns3 with multiple processes
- 极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等 插件
猜你喜欢
随机推荐
LeetCode 2353. 设计食物评分系统 维护哈希表+set
STM32LL library - USART interrupt to receive variable length information
5. Transaction management
永久更改pip源
Unity-PlayMaker
光栅区域衍射级数和效率的规范
Manifest merger failed : Attribute [email protected] value=
二叉树的遍历:递归法/ 迭代法/ 统一迭代法(强QAQ)
Happy, 9/28 scene collection
net start mysql 服务名无效。
第二十九章:树的基本概念和性质
Litestar 4D – WebCatalog 7:全自动数据管理
MATLAB drawing command fimplicit detailed introduction to drawing implicit function graphics
mininet hosts talk to real internet
unity 和C# 一些官方优化资料
udp transparent proxy
Unity-Post Processing
数学工具-desmos 图形曲线
Evaluation multipath weswood congestion control on ns3
十天学习Unity3D脚本(一)九个回调