长沙理工大学第十二届ACM大赛-重现赛 B 日历中的数字 (实现)

链接:https://ac.nowcoder.com/acm/contest/1/B
来源:牛客网

全屏查看题目
 
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

ElemenT马上就要毕业了,他打开日历看了看时间。发现日历上的日期都是2017-04-04这样的格式的,月和日如果不足2位数,前面都会补充0。
给定一个年份和月份,ElemenT把那个月的日期都按上述格式写到纸上,他现在想知道某种数字出现了多少次。

输入描述:

多组输入
每组输入一行,有3个数字y,m,x(1000<=y<=3000,1<=m<=12,0<=x<=9),分别代表年份,月份,和他想知道哪个数字出现的次数。

输出描述:

每组输出一个整数,表示数字x在这个月的日期里出现了多少次。
示例1

输入

复制
2017 4 4
2000 1 0

输出

复制
33
我也不知道,2333

说明

第一组样例中,日中有数字4的为2017-04-04,2017-04-14,2017-04-24,4月一共有30天,因为月份中有4,所以数字4一共出现了30 + 3 = 33次



思路:
循环一遍月份的每一天,
对于每一天,加上年份中的x的个数、月份中的x的个数、日份中的x的个数、。
注意两点:
1、 2月要根据年份是否是闰年来判断
2、 如果x是0,要注意"月和日如果不足2位数,前面都会补充0。"
  也就是,如果日和月是个位数,对于x是0的情况,返回值要多加一个1.

细节见代码:、
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

int y,m,x;
ll check(int y,int x)
{
    int res=0;
    int l=0;
    while(y)
    {
        l++;
        // cout<<y<<" ";
        if((y%10)==x)
        {
            // cout<<y<<" "<<x<<endl;
            res++;
            // return 1;
        }
        y/=10;
    }    
    if(l==1&&x==0)
    {
        res++;
    }
    // db()
    return res;
    // return 0;
}
int a[maxn]={0,31,0,31,30,31,30,31,31,30,31,30,31,30,30,30,30,30,30,30,30,30};
bool isrun()
{
    if((y%400)==0)
    {
        return 1;
    }else if((y%4)==0&&(y%100)!=0)
    {
        return 1;
    }else
    {
        return 0;
    }
}
int main()
{
    // freopen("D:\code\text\input.txt","r",stdin);
    //freopen("D:\code\text\output.txt","w",stdout);
    while(cin>>y>>m>>x)
    {
        int ans=0;
        if(isrun())
        {
            a[2]=29;
        }else
        {
            a[2]=28;
        }
        ans+=a[m]*check(y,x);
        ans+=a[m]*check(m,x);
        repd(i,1,a[m])
        {
            ans+=check(i,x);
        }
        cout<<ans<<endl;

    }
    
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '
');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}




本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/10994629.html