根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:,空间复杂度:
#include <stdio.h> int IsLeapYear(int year) { if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1; else return 0; } int main() { int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; int My_year; scanf("%d",&My_year); int My_month; scanf("%d",&My_month); int My_day; scanf("%d",&My_day); if(IsLeapYear(My_year)) month[1]++; int sum = 0; for(int i = 0;i < My_month - 1;i++) { sum += month[i]; } sum += My_day; printf("%d",sum); return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int a, b; int year, month, day; int sum = 0; scanf("%d %d %d", &year, &month, &day); int days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//平年 int days2[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//闰年 for (int i = 1; i < month; i++) { if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))) { sum += days2[i - 1]; } else sum += days[i - 1]; } printf("%d", sum + day); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int calculate_day(int year, int mouth) { int days; switch (mouth) { case 1: days = 31; break; case 2: if (year % 400 == 0) { days = 29; } else if ((year % 4 == 0) && (year % 100 != 0)) { days = 29; } else{ days = 28; } break; case 3: days = 31; break; case 4: days = 30; break; case 5: days = 31; break; case 6: days = 30; break; case 7: days = 31; break; case 8: days = 31; break; case 9: days = 30; break; case 10: days = 31; break; case 11: days = 30; break; case 12: days = 31; break; } return days; } // 4年一润,闰年28天,平时29天,1 3 5 7 8 10 12 共31天,2 4 6 9 11 共30天 int main() { int year; int mouth; int day; int dates[12]; int maxday = 0; while (scanf("%d %d %d", &year, &mouth, &day) != EOF) { // 注意 while 处理多个 case // 64 位输出请用 printf("%lld") to memset(dates, 0, sizeof(dates)); for (int i = 1; i <= 12; i++) { dates[i - 1] = calculate_day(year, i); // printf("%d ", dates[i - 1]); } // printf("\n"); for (int i = 1; i < mouth; i++) { maxday += dates[i - 1]; } maxday += day; printf("%d\n", maxday); } return 0; }
#include <stdio.h> int leapyear(int year)//判断是否为闰年 { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int MonthDay(int year, int month, int day)//用数组存储每月的天数 { int daysmonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //是闰年就修改二月的值 if (leapyear(year)) { daysmonth[2] = 29; } int DayCount = day;//初始化天数为当前给定天数 for (int i = 1; i < month; ++i)//遍历每月的天数累加到给定天数上 { DayCount += daysmonth[i]; } return DayCount; } int main() { int year, month, day; scanf("%d %d %d", &year, &month, &day); int size = MonthDay(year, month, day); printf("%d\n", size); return 0; }
#include <stdio.h> //暴力方法 int main() { int year; int month; int day; scanf("%d %d %d",&year,&month,&day); int sum =0; //1 3 5 7 8 10 12 ——31天 //4 6 9 11 ——30天 //闰年2月 ——29天 //平年2月 ——28天 //如果year是闰年 2000 11 15 if((year%4==0&&year%100!=0)||(year%400==0)) { switch(month) { case 1: sum=day; break; case 2: sum=31+day; break; case 3: sum=31+29+day; break; case 4: sum=31+29+31+day; break; case 5: sum=31+29+31+30+day; break; case 6: sum=31+29+31+30+31+day; break; case 7: sum=31+29+31+30+31+30+day; break; case 8: sum=31+29+31+30+31+30+31+day; break; case 9: sum=31+29+31+30+31+30+31+31+day; break; case 10: sum=31+29+31+30+31+30+31+31+30+day; break; case 11: sum=31+29+31+30+31+30+31+31+30+31+day; break; case 12: sum=31+29+31+30+31+30+31+31+30+31+30+day; break; } } else { switch(month) { case 1: sum=day; break; case 2: sum=31+day; break; case 3: sum=31+28+day; break; case 4: sum=31+28+31+day; break; case 5: sum=31+28+31+30+day; break; case 6: sum=31+28+31+30+31+day; break; case 7: sum=31+28+31+30+31+30+day; break; case 8: sum=31+28+31+30+31+30+31+day; break; case 9: sum=31+28+31+30+31+30+31+31+day; break; case 10: sum=31+28+31+30+31+30+31+31+30+day; break; case 11: sum=31+28+31+30+31+30+31+31+30+31+day; break; case 12: sum=31+28+31+30+31+30+31+31+30+31+30+day; break; } } printf("%d",sum); return 0; }
#include <stdio.h> int main() { int month_day[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year, month, day; scanf("%d %d %d",&year, &month, &day); if(year%400==0 || (year%4==0 && year%100!=0)){ month_day[1]++; } int res=0; for(int i=0; i<month-1; i++){ res+=month_day[i]; } res+=day; printf("%d",res); return 0; }
#include<stdio.h> int main() { int month_day[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year = 0; int month = 0; int day = 0; scanf("%d%d%d", &year, &month, &day); if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { month_day[1] = 29; } int count = 0; for (int i = 0; i < month - 1; i++) { count += month_day[i]; } count += day; printf("%d\n", count); return 0; }
#include<stdio.h> int main() { int year=0,month=0,day=0; scanf("%d %d %d",&year,&month,&day); int n=0; //判断月份是否小于等于2 if(month<=2) { if(month==1) { printf("%d",day); } else { printf("%d",day+31); } } else { //判断是不是闰年 if((year%4==0&&year%100!=0)||(year%400==0)) { //是闰年,2月29天 n=31+29; } else { n=31+28; } //七月之前包括七月 if(month<=7) { //3 5 7 31 month--; while((month-2)>0) { if(month%2==1)//奇数月 { n=n+31; } else { n=n+30; } month--; } printf("%d ",n+day); } else//七月之后 { month--; n=n+31*3+30*2; while((month-7)>0) { if(month%2==1)//奇数月 { n=n+30; } else { n=n+31; } month--; } printf("%d ",n+day); } } return 0; }我的解法好菜
#include<stdio.h> int panduan(int year) { if((year%4==0&&year%100!=0)||(year%400==0&&year%100==0)) return 0; else return 1; } int main() { int year,month,day,sum=0; scanf("%d %d %d",&year,&month,&day); int ret=panduan(year); int brr[]={31,28,31,30,31,30,31,31,30,31,30,31}; if(ret==0) brr[1]++; for(int i=0;i<month-1;i++) { sum+=brr[i]; } sum+=day; printf("%d",sum); return 0; }
#include<stdio.h> int main() { int y = 0; int m = 0; int d = 0; scanf("%d %d %d", &y, &m, &d); int sum = 0; // 首元素为0 使后面月份与下标对应 // 因为12月份循环不会进入 所以只需11个月的天数 int day[12] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30}; // 闰年二月加一天 if((y%4==0 && y%100!=0) || y%400==0) { day[2] += 1; } for(int i = 0;i < m;i++) { sum += day[i]; } // 循环结束加上天数 // 12月的天数就是输入的d sum += d; printf("%d\n", sum); return 0; }
#include<stdio.h> int main() { int leap_year[] = {31,29,31,30,31,30,31,31,30,31,30,31}; int noneleap_year[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int year, mon, day, sum_day; year = mon = day = 0; scanf("%d %d %d ", &year, &mon, &day); if(year%4 == 0 && year%100 != 0 || year%400 == 0) { sum_day = 0; for(int i = 0; i<mon-1; i++) { sum_day += leap_year[i]; } sum_day += day; printf("%d", sum_day); } else { sum_day = 0; for(int i = 0; i<mon-1; i++) { sum_day += noneleap_year[i]; } sum_day += day; printf("%d", sum_day); } return 0; }
#include <stdio.h> int is_leap_year(int year) { if ((year % 4 == 0 && year % 100 !=0) || (year % 400 == 0) ) return 1; return 0; } int main() { int month_day[] = {0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 , 31}; int year,month,day; while( ~scanf("%d %d %d",&year, &month, &day) ) { int total_day = day; if( is_leap_year(year) == 1 && month > 2) { total_day++; } for(int i = 0; i<month; i++) { total_day += month_day[i]; } printf("%d\n", total_day); } return 0; }