林惜城


私信TA

用户名:reminder

访问量:27604

签 名:

等  级
排  名 94
经  验 8488
参赛次数 0
文章发表 95
年  龄 0
在职情况 学生
学  校 西安电子科技大学
专  业

  自我简介:

哈姆


解题思路:

既然是C++,用class代替struct也是很正常的吧。计算第几天就是建一个数组存放每个月有几天,然后把前面的月的天数加起来,加上day,最后闰年+1。


注意事项:

好像也没啥可说的。


参考代码:

#include <iostream>

using namespace std;

const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 定义成13是为了方便对应数组下标

class Date {
	private:
		short year; // 年
		short month; // 月
		short day; // 日
	public:
		// 构造函数
		Date(short year, short month, short day) {
			this->year = year;
			this->month = month;
			this->day = day;
		}
		// 析构函数
		virtual ~Date() {
		}
		void countDay() {
			short count = day; // 计数
			for(int i = 1; i < month; i++) {
				count += days[i];
			}
			if((!(year % 4) && (year % 100)) || !(year % 400)) {
				count++; // 闰年则+1
			}
			cout << count << endl; // 可以return但没必要
		}
};

int main() {
	short year = 0;
	short month = 0;
	short day = 0;
	cin >> year >> month >> day; // 读输入
	Date date(year, month, day);
	date.countDay();
	return 0;
}


 

0.0分

1 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区