当前位置:网站首页>"Array" look-up table method (leap year)
"Array" look-up table method (leap year)
2022-08-02 16:03:00 【white 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 Let their survival period longer,在数据区,const Let it do not change,Ability to get smaller.
//查表法
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
//The total number of days are the current digital and input the number of days.
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 ,So to judge whether a leap year,如果小于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);
// Crossing the line and input wrong judgment
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;
}
边栏推荐
- 2021-06-06
- 使用1D-1D EPE的光波导布局设计工具
- golang的内存相关内容
- C语言函数调用过程-汇编分析
- Optisystem应用:光电检测器灵敏度建模
- 光波导的入射耦合和出射耦合区域
- The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories
- 基类和派生类的关系【继承】/多态和虚函数/【继承和多态】抽象类和简单工厂
- VirtualLab Fusion中的可视化设置
- 【Solidity智能合约基础】-- 基础运算与底层位运算
猜你喜欢
随机推荐
C#高级教程
tcp transparent proxy (IP_TRANSPARENT)
十天学习Unity3D脚本(一)九个回调
理解:野指针,空指针,失效指针。
unity 和C# 一些官方优化资料
远程连接Ubuntu中的Mysql
图解MESI(缓存一致性协议)
Manifest merger failed : Attribute [email protected] value=
第三十三章:图的基本概念与性质
lua编程
【solidity智能合约基础】节约gas的利器--view和pure
创建系统还原点及恢复
OpenPose 基本理念
C语言函数调用过程-汇编分析
Oauth2.0 补充
CDH(computational Diffie-Hellman)问题以及与离散对数、DDH问题的区别
仿真结果的格式&定制
Ubuntu通过apt安装Mysql
从FAST TCP到POWERTCP
CDH (computational Diffie-Hellman) problem and its differences with discrete logarithm and DDH problems