1 条题解

  • 0
    @ 2026-10-23 18:34:35

    P76016 下一特别年份 — 题解

    专题: 模拟与枚举
    适用题目: 新生 OJ 本地题包中的 P76016,不要直接用于外站同名题。

    解题思路

    从 y+1 起枚举年份,将四个十进制数位放进标记表,发现数字重复就继续。

    复杂度

    最坏枚举范围内的若干年,每年 O(4)O(4)。

    易错点

    必须严格大于输入年份,不能把输入本身作为候选。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        int y; scanf("%d",&y);
        for (y++; ; y++) {
            int z=y, seen[10]={0}, good=1;
            for(int i=0;i<4;i++){int digit=z%10; z/=10; if(seen[digit]) good=0; seen[digit]=1;}
            if(good) {printf("%d\n",y); break;}
        }
        return 0;
    }
    

    C++17 参考程序

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    #define nl '\n'
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int y;
        cin>>y;
        do{
            y++;
            string s=to_string(y);
            set<char> u(s.begin(),s.end());
            if(u.size()==4)break;
        }
        while(true);
        cout<<y<<nl;
        return 0;
    }
    

    题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。

    • 1

    信息

    ID
    1785
    时间
    2000ms
    内存
    256MiB
    难度
    1
    标签
    提交数
    0
    已通过
    0
    上传者