有大佬帮忙看看这个哪里错了吗
#include <stdio.h>void splitdate(int year,int yearday,int *month,int *day);
int main()
{
const char daytab={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int year,yearday,month,day;
scanf("%d%d",&year,&yearday);
void splitdate(int year,int yearday,&month,&day);
printf("%d %d",month,day);
}
void splitdate(int year,int yearday,int *month,int *day)
{
int k,leap;
const char daytab={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
if(year % 4==0 && year % 100==!0||year % 400==0)
{
leap = 2;
}
else
{
leap = 1;
}
for (k=1;daytab<day;k++)
{
yearday-=daytab;
}
*month = k;
*day = yearday;
}
问题:
给定一个年份y和整数d,问这一年的第d天是几月几日?
c语言
本帖最后由 jackz007 于 2021-3-11 19:08 编辑
#include <stdio.h>
void splitdate(int year , int yearday , int * month , int * day)
{
int d , m , leap ;
const char daytab={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}} ;
leap = 0 ;
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) leap ++ ;
for(d = 0 , m = 1 ; m < 13 && d + daytab <= yearday ; d += daytab , m ++) ;
* month = m ;
* day = yearday - d ;
}
int main(void)
{
int year , yearday , month , day ;
scanf("%d%d" , & year , & yearday) ;
splitdate(year , yearday , & month , & day) ;
printf("%d %d\n" , month , day) ;
}
编译、运行实况
D:\0002.Exercise\C>g++ -o x x.c
D:\0002.Exercise\C>x
2020 31
1 31
D:\0002.Exercise\C>x
2020 366
12 31
D:\0002.Exercise\C>x
2021 32
2 1
D:\0002.Exercise\C>x
2021 365
12 31
D:\0002.Exercise\C> 谢谢大佬
页:
[1]