标题:被这个题搞自闭了
只看楼主
程序小新
Rank: 2
等 级:论坛游民
帖 子:6
专家分:13
注 册:2020-4-16
得分:3 
我也做过一个这样类似的题,不过我自己写的太啰嗦,贴给你看一下,希望可以帮到你,剩余天数减一下就好了。
/*
题目:输入某年某月某日,判断这一天是这一年的第几天?
*/
#include<stdio.h>
void main()
{
    int year,month,day;
    int i = 0;
    int sum_1,sum_2,sum_3,sum_4,sum_5,sum_6,sum_7,sum_8,sum_9,sum_10,sum_11;
    sum_1 = 31;sum_2=sum_1+28;sum_3=sum_2+31;sum_4=sum_3+30;sum_5=sum_4+31;
    sum_6=sum_5+30;sum_7=sum_6+31;sum_8=sum_7+31;sum_9=sum_8+30;
    sum_10=sum_9+31;sum_11=sum_10+30;
    printf("年:");
    scanf("%d",&year);
    printf("月:");
    scanf("%d",&month);
    printf("日:");
    scanf("%d",&day);
    if(year%4 == 0)
    {
        switch(month)
        {
            case 1:
                 i=day;
                 break;
            case 2:
                 i=sum_1 + day;
                 break;
            case 3:
                 i=sum_2 + day + 1;
                 break;
            case 4:
                 i=sum_3 + day + 1;
                 break;
            case 5:
                 i=sum_4 + day + 1;
                 break;
            case 6:
                 i=sum_5 + day + 1;
                 break;
            case 7:
                 i=sum_6 + day + 1;
                 break;
            case 8:
                 i=sum_7 + day + 1;
                 break;
            case 9:
                 i=sum_8 + day + 1;
            case 10:
                 i=sum_9 + day + 1;
                 break;
            case 11:
                 i=sum_10 + day + 1;
                 break;
            case 12:
                 i=sum_11 + day + 1;
                 break;
        }
    }
    else
    {
        switch(month)
        {
            case 1:
                 i=day;
                 break;
            case 2:
                 i=sum_1 + day;
                 break;
            case 3:
                 i=sum_2 + day;
                 break;
            case 4:
                 i=sum_3 + day;
                 break;
            case 5:
                 i=sum_4 + day;
                 break;
            case 6:
                 i=sum_5 + day;
                 break;
            case 7:
                 i=sum_6 + day;
                 break;
            case 8:
                 i=sum_7 + day;
            case 9:
                 i=sum_8 + day;
                 break;
            case 10:
                 i=sum_9 + day;
                 break;
            case 11:
                 i=sum_10 + day;
                 break;
            case 12:
                 i=sum_11 + day;
                 break;
        }
    }
    printf("这一天是这一年的第%d天\n",i);
}
2020-04-16 16:49
程序小新
Rank: 2
等 级:论坛游民
帖 子:6
专家分:13
注 册:2020-4-16
得分:0 
这个是引自脚本之家的答案:
1、程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,
特殊情况,闰年且输入月份大于3时需考虑多加一天。
2、程序源代码:
#include<stdio.h>
void main()
{
    int day,month,year,sum,leap;
    printf("\nplease input year,month,day\n");
    scanf("%d,%d,%d",&year,&month,&day);
    switch(month)//先计算某月以前月份的总天数
    {
         case 1:sum=0;break;
         case 2:sum=31;break;
         case 3:sum=59;break;
         case 4:sum=90;break;
         case 5:sum=120;break;
         case 6:sum=151;break;
         case 7:sum=181;break;
         case 8:sum=212;break;
         case 9:sum=243;break;
         case 10:sum=273;break;
         case 11:sum=304;break;
         case 12:sum=334;break;
         default:printf("data error");break;
}
    sum=sum+day;  //再加上某天的天数
     if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
      leap=1;
     else
      leap=0;
    if(leap==1&&month>2)//如果是闰年且月份大于2,总天数应该加一天
        sum++;
    printf("It is the %dth day.",sum);}
2020-04-16 16:50
wfzz001
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-4-23
得分:0 
我也是菜鸟。我刚刚试着编了一下,试运行好像没有问题。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<iostream>

int main()
{
int m[13],moon,year,day,i,mi;
  m[1]=31;
  m[2]=28;
  m[3]=31;
  m[4]=30;
  m[5]=31;
  m[6]=30;
  m[7]=31;
  m[8]=31;
  m[9]=30;
  m[10]=31;
  m[11]=30;
  m[12]=31;
  mi=0;
  printf("请输入四位数年份:");
  scanf("%d",&year);
   printf("请输入月份:");
  scanf("%d",&moon);
    printf("请输入日期:");
  scanf("%d",&day);
  if(year%4==0)
  {
      m[2]=29;
      if(moon==1)
      {
          printf("今年已经过去%d天",day);
          mi=366-day;
          printf(",还剩下%d天。\n",mi);
    }
    else
      {
      for(i=1;i<moon;i++)
      {
          mi=mi+m[i];
      }
      mi=mi+day;
    printf("今年已经过去%d天",mi);
          mi=366-mi;
          printf(",还剩下%d天。\n",mi);
      
    }
  }
  else
  {
      if(moon==1)
      {
          printf("今年已经过去%d天",day);
          mi=365-day;
          printf(",还剩下%d天。\n",mi);
    }
    else
      {
      for(i=1;i<moon;i++)
        {
          mi=mi+m[i];
        }
      mi=mi+day;
    printf("今年已经过去%d天",mi);
          mi=365-mi;
          printf(",还剩下%d天。\n",mi);
      
    }
  }
system("pause");
    return 0;
}

[此贴子已经被作者于2020-4-23 14:50编辑过]

2020-04-23 14:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501030-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.543841 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved