【C++】找出給定天數的月數

題目說明

找出某一年中,大於給定天數的月份數;

輸入描述:

第一行:輸入N,表示測試樣例個數。【N<100000】

第二行:樣例1數據-》year 和 date

……………… ………………………………………………………

第N行 : 樣例N數據-》year 和 date

輸出描述:

第一行:輸出樣例1數量

第二行:輸出樣例2數量

……………… ………………………………

第N行 :輸出樣例N數量

實例1

輸入:

1

2017 30

輸出:

7

實例2

輸入:

2

2018 30

2019 4

輸出:

7

12

代碼演示

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

class Solution
{
 public:
    bool judgeYear(int year)
    {
	    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) 
	    {
	       return true; 
	    }
	    return false;
	}

    int getCount(int year, int date)
    {
	    int data_count[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	    if (judgeYear(year))
	    {
	    	data_count[1] = 29;
    	}
    
	    int count = 0;
	    for (int i = 0; i < 12; i++)
	    {
	    if (data_count[i] > date)
	    {
	    count++;
	    }
	    }
	    return count;
    }
    
    void fun()
    {
	    int count;
	    cin >> count;
	    
	    int tmp;
	    vector<int> count_ve;
	    vector<pair<int, int>> tmp_ve; 
	    pair<int, int> pair_p;
	    
	    for (int i = 0; i < 2 * count; i++)
	    {
		    cin >> tmp;
		    i % 2 == 0 ? pair_p.first = tmp : pair_p.second = tmp;
		    if (i % 2 != 0)
		    {
		    	tmp_ve.push_back(pair_p);
		    }
	    }
	    
	    for (int i = 0; i < count; i++)
	    {
		    int tmp = getCount(tmp_ve[i].first, tmp_ve[i].second);
		    count_ve.push_back(tmp);
	    }
	    
	    vector<int> ::iterator it = count_ve.begin();
	    for (it; it != count_ve.end(); it++)
	    {
	    	cout << *it << endl;
	    }
	}
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章