Manchester


私信TA

用户名:wenyajie

访问量:314778

签 名:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

等  级
排  名 1
经  验 63010
参赛次数 1
文章发表 188
年  龄 0
在职情况 学生
学  校 Xiamen University
专  业 计算机科学

  自我简介:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

解题思路:
用一个长度为12的数组,记录每个月的天数,除了2月先不记录;

输入日期,判断闰年2月赋值29,平年28;

计算是第几天,输出

参考代码:

#include <stdio.h>
#include <iostream>

using namespace std;

class riqi {
private:
    int    year;
    int    month;
    int    day;

public:
    riqi( int y, int m, int d )
    {
        year    = y;
        month    = m;
        day    = d;
    }
    void count();
};
void riqi::count()
{
    int    n = 0;
    int    a[12];
    a[0]    = 31;
    a[1]    = 0;
    a[2]    = 31;
    a[3]    = 30;
    a[4]    = 31;
    a[5]    = 30;
    a[6]    = 31;
    a[7]    = 31;
    a[8]    = 30;
    a[9]    = 31;
    a[10]    = 30;
    a[11]    = 31;


    if ( year % 100 == 0 )   //判断年数为2000这样的年份
    {
        if ( year % 400 == 0 )
            a[1] = 29;
        else
            a[1] = 28;
    }

    if ( year % 4 == 0 && year % 100 != 0 ) //能被4整除但不能被100整除 
        a[1] = 29;

    if ( year % 4 != 0 )  
        a[1] = 28;


    for ( int i = 0; i < month - 1; i++ )  //数组下标从0开始,所以month-1
        n = n + a[i];

    n += day;

    cout << n;
}


int main()
{
    int Y, M, D;
    cin >> Y;
    cin >> M;
    cin >> D;

    riqi A( Y, M, D );


    A.count();
    return(0);
}

几个月前写的,翻出来弄题解,讲的思路没有写的时候清晰~

别忘点赞哦-.-


 

0.0分

38 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

你们这表达是不是太麻烦了点,判断闰年我都记住这一个if就行了。
#include<stdio.h>
struct much
{
	int year;
	int mouth;
	int day;
};
int main()
{
	int sum=0,i;
	struct much today;
	int mou[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	scanf("%d %d %d",&today.year,&today.mouth,&today.day);
	if((today.year%4==0&&today.year%100!=0)||today.year%400==0)
	{
		mou[1]=29;
	}
	for(i=0;i<12;i++)
		{
			if(today.mouth!=i+1)
			{
				sum+=mou[i];
			}else
			{
				sum+=today.day;
				break;
			}
		}
	printf("%d",sum);
    return 0;
}
2018-12-27 19:20:31
这个用开关语句switch会不会更简洁明了?
2018-12-04 01:00:35