POJ2080日历问题

基本思路:

1.确定星期几(先需确定2000.1.1是星期几)

2.确定年:闰年366天,否则365天

3.确定月:每个月长短不同

4.确定日

#include <iostream>
using namespace std;
//判断是闰年还是平年
int type(int m)
{
    if(m % 4 != 0 ||(m % 100 == 0 && m % 400 != 0))
    return 0;
    else
    return 1;
}
//定义个年份的数组,year[0]表示平年,year[1]表示闰年
int year[2] = {365, 366};
//定义一个二维数组,month[0]表示平年,mont[1]表示闰年
int month[2][12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
                    31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//表示一周
char week[7][10] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
int main()
{
    int days;
    int i;//表示年份
    int j;//表示月份
    while(cin >> days && days != -1)
    {
        int dayofweek = days % 7;
        for(i = 2000; days >= year[type(i)]; ++i)
        days -= year[type(i)];

        for(j = 0; days >= month[type(i)][j]; ++j)
        days -= month[type(i)][j];

        cout << i << "-";
        cout.width(2);//设置显示的宽度
        cout.fill('0');//当宽度不足2时,用的填充字符
        cout << j + 1 << "-" ;
        cout.width(2);
        cout.fill('0');
        cout << days + 1 << " " << week[dayofweek];
        cout << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值