您的位置:首页 > 编程语言

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

2017-05-23 17:39 253 查看
题目很简单。枚举后面四位就行了。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iterator namespace