如何在排序后减去天数 [英] How to Subtract Days after Sorting
问题描述
按照人们的生日排序后,我如何减去两个生日以找出两者之间的天数,以便知道某人左边或右边的人的生日最接近? br/>
例如:
1月31日---- 2月2日---- 2月15日
Person1 --- Person2 --- Person3
结果:Person2的生日最接近person1.
如果您想知道the年,则除非某人有b天,否则2月仅算作28天.
我应该将1月= 1,就像我应该放
#define 1月1日,然后每月执行一次?
那我可以做些什么:
if (month == JANUARY) dayofyear = day; else if (month == MARCH) dayofyear = day+28+31; ...and so on
*仅C编程.
谢谢,我终于想到了这样的东西:
int dayOfYear = dayOfMonth + monthOffset[monthNumber];
但是没有意识到大约12月31日和1月1日.人们是按照生日而不是年龄来排序的,所以问题在于忽略了这一年,所以我猜想这一年没有发挥作用.我想我只需要拿出另一个函数或if语句来表示12月
您的麻烦是什么?
>
您可以基于一年中的某天(可以通过简单的表格查询来计算):
int dayOfYear = dayOfMonth + monthOffset[monthNumber];
其中monthOffset是一个数组,其中包含12个常数,从0和31开始.您可能有两个常数,一个代表leap年,一个正则.
但是,新年前后您可能会遇到问题.如果时间跨度超过半年,则应将其镜像到下一年.
:)
After people are sorted in order of their birthdays, how can I subtract two birthdays to find out the days in between so that I can know if the person to the left or the right of someone has the closest birthday to them?
example:
JAN 31 ---- FEB 2 ---- FEB 15
Person1 --- Person2 --- Person3
Result: Person2 has closest birthday to person1.
In case you wonder about the leap year, February is only be counted as 28 days, unless someone has that b-day.
Should I make January = 1, like should I put
#define JANUARY 1 and do that for each month?
Then could I do something like:
if (month == JANUARY) dayofyear = day; else if (month == MARCH) dayofyear = day+28+31; ...and so on
*C programming only.
Thanks, yeah I finally thought of something like that:
int dayOfYear = dayOfMonth + monthOffset[monthNumber];
but didn''t realize about December 31 and Jan 1. People are being sorted by their birthdays, not age, so the problem says to ignore the year for that so I guess the year doesn''t come into play. I guess I will just have to come up with another function or if statement for December解决方案What is your trouble about?
A little more than aswitch
will do the job.
Hi,
you can base this on day-of-year, which can be calculated as a simple table look-up:
int dayOfYear = dayOfMonth + monthOffset[monthNumber];
where monthOffset is an array holding 12 constants, starting with 0 and 31. You may have two of them, one for leap years, one regular.
However, you have a potential problem around New Year. When a time span exceeds half a year, you should mirror it to the next year.
:)
这篇关于如何在排序后减去天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!