2009-08-31

http://anond.hatelabo.jp/20090831113633

Cだとこんな感じか。

#include <stdio.h&gt;
int isLeap(int year) {
  return ((year%400 == 0) || (year%4 == 0) &amp;amp;&amp;amp; (year%100 != 0)) ? 1 : 0;
}

int todays(int year, int month, int day) {
  int MonthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int days = 0;
  int i = 0;
  days += ((month &gt; 2) &amp;amp;&amp;amp; isLeap(year)) ? 1 : 0;
  for (i = 0; i < month-1; i++) {
    days += MonthDays[i];
  }
  days += day;
  /* printf("leap %d days %d\n", isLeap(year), days); */
  return days;
}

int cntLeap(Syear, Eyear) {
  int i = 0;
  int cnt = 0;
  for (i = Syear; i < Eyear; i++) {
    cnt += isLeap(i);
  }
  return cnt;
}

int main() {
  int year = 2009;
  int month = 6;
  int day = 4;
  long days = 0;
  unsigned long long sec = 0;
  int DaysSec = 60 * 60 * 24;

  days += (year-1)*365 + cntLeap(0, year);
  days += todays(year, month, day);
  days -= 1;

  sec = days * DaysSec;
  printf("%d %d %d:days %d: sec %llu\n", year, month, day, days, sec);
  /* 2009 6 4:days 733566: sec 18446744072665144576 */

  return 0;
}
記事への反応 -
  • ちょっぴりムズムズしたので。 class sec { public static void main(String args[]){ int year = 2009; int month = 6; int day = 4; //日付を入力してください。 int yday[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304...

    • Cだとこんな感じか。 #include &lt;stdio.h&gt;int isLeap(int year) { return ((year%400 == 0) || (year%4 == 0) &amp;amp;&amp;amp; (year%100 != 0)) ? 1 : 0;}int todays(int year, int month, int day) { int MonthDays[] = {31, 28, 31, 30, 31, 30, ...

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん