18326638710 发表于 2013-11-16 19:03:51

小甲鱼作业:计算今天是今年的第几天。。。


#include <stdio.h>

void main()
{
int month, d, day, year, leap;
printf("please a date: \n");
scanf("%d %d %d",&year, &month, &d);
if ( year % 4 == 0 )
{
if ( year % 100 == 0 )
{
if ( year % 400 == 0 )
{
leap = 1;
}
else leap = 1;
}
}
else leap = 0;
if ( leap )
{
if ( month > 3 )
{
day = 31;
printf("This is %d year %d days\n", year,(month-1)*31+d);pause;
}
else if ( month = 2 )
{
day = 28;
printf("This is %d year %d days\n", year,31+d);pause;
}
}else
switch (month)
{
case 1: printf("This is %d year %d days\n", year,d); break;
case 2: printf("This is %d year %d days\n", year,31+d); break;
case 3: printf("This is %d year %d days\n", year,61+d); break;
case 4: printf("This is %d year %d days\n", year,2*31+30+d); break;
case 5: printf("This is %d year %d days\n", year,2*(30+31)+d); break;
case 6: printf("This is %d year %d days\n", year,3*31+2*30+d); break;
case 7: printf("This is %d year %d days\n", year,3*(30+31)+d); break;
case 8: printf("This is %d year %d days\n", year,4*31+3*30+d); break;
case 9: printf("This is %d year %d days\n", year,5*31+3*30+d); break;
case 10: printf("This is %d year %d days\n", year,5*31+4*30+d); break;
case 11: printf("This is %d year %d days\n", year,6*31+4*30+d); break;
case 12: printf("This is %d year %d days\n", year,6*31+5*30+d); break;
default: printf("input error!!!!!\n");
}

}
这是我写的代码,我觉得代码量太多了,关于这个问题能有好的办法么??请大神帮忙。。。这是小甲鱼视频里的题目。。

仰望天上的光 发表于 2013-11-16 19:03:52

#include<stdio.h>
int main(void) {
    int year,month,day,days,i,d;
        printf("please a date: \n");       
        scanf("%d %d %d",&year, &month, &day);       
    days=0;
    for(i=1;i<month;i++) {
      switch(i) {
                case 1: case 3: case 5:
                case 7: case 8: case 10:
                case 12:d=31;break;
                case 4: case 6: case 9:
                case 11:d=30;break;
                case 2:d=((year%4==0&&year%100!=0||year%400==0)?29:28);
      }
      days+=d;
    }
    printf("%d年%d月%d日是这一年的第%d天。\n",year,month,day,days+day);
}

18326638710 发表于 2013-11-16 21:49:29

仰望天上的光 发表于 2013-11-16 21:01 static/image/common/back.gif


哦,谢谢。学习了。。。^_^

18326638710 发表于 2013-11-16 22:07:10

18326638710 发表于 2013-11-16 21:49 static/image/common/back.gif
哦,谢谢。学习了。。。^_^

E:\test.jpg

下弦月之夜_夜末 发表于 2013-11-17 08:07:58

//求今天是今年的第几天
#include "stdio.h"
int main(void)
{
      int a={31,28,31,30,31,30,31,31,30,31,30,31},i,yue,ri,sum=0;
      printf("请输入月份:");
      scanf("%d",&yue);
      printf("请输入日数:");
      scanf("%d",&ri);
      for(i=0;i<yue;i++)
                sum=sum+a;
      printf("今天是今年的第%d天",(sum+ri));
      return 0;
}

下弦月之夜_夜末 发表于 2013-11-17 08:26:01

#include<stdio.h>
int main(void) {
    int year,month,day,days,i,d;
      printf("please a date: \n");      
      scanf("%d %d %d",&year, &month, &day);      
    days=0;
    for(i=1;i<=month;i++)   //mouth前面应该加个=
        {
      switch(i)
                {
                case 1:
                                case 3:
                                case 5:
                case 7:
                                case 8:
                                case 10:
                case 12:d=31;break;
                case 4:
                                case 6:
                                case 9:
                case 11:d=30;break;
                case 2:d=((year%4==0&&year%100!=0||year%400==0)?29:28);
      }
      days+=d;
    }
    printf("%d年%d月%d日是这一年的第%d天。\n",year,month,day,days+day);
return 0;
}

风吹绿野 发表于 2013-11-17 12:59:06

不错不错,学习了
页: [1]
查看完整版本: 小甲鱼作业:计算今天是今年的第几天。。。