【noip题目代码】回文日期;vetor,模拟,iterator

题目很简单。枚举后面四位就行了。

#include<vector>
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int moths[13] = { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 //1 true
 //0 flase
int isHuiwen(int num)
{
    int sum = 0;
    int oldNum = num;

    while (num != 0)
    {
        sum *= 10;
        sum+=num % 10;
        num /= 10;
    }

    if (oldNum == sum)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

bool isRunYear(int year)
{
    if (year % 4 == 0 && year % 100 != 0)
    {
        return true;
    }
    if (year % 400 == 0)
    {
        return true;
    }
}

bool isRightMothDay(int num)
{
    //2012 12 22
    int year = num / 10000;
    int moth=num / 100 % 100;
    int day = num % 100;

    if (moth == 2)
    {
        if (isRunYear(year))
        {
            moths[2] = 29;
        }
        else
        {
            moths[2] = 28;
        }
    }

    if (day > moths[moth])
    {
        return false;
    }
    else
    {
        return true;
    }
    return false;
}

int main()
{
    vector<int> v;
    int ans = 0;
    int date1 = 0;
    int date2 = 0;

    cin >> date1
        >> date2;

    for (int i = date1 / 10000; i<=date2/10000; i++)
    {
        for (int j= 0; j <= 1231; j++)
        {
            int temp = 0;
            if (isHuiwen(temp = i * 10000 + j))
            {
                v.push_back(temp);
            }
        }
    }

    for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    {
        if (*it >= date1 && *it <= date2)
        {
            if (isRightMothDay(*it))
            {
                ans++;
            }
        }
    }

    cout << ans;

    system("pause");

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值