Codeforces Round #104 (Div.2) 补题

B. Lucky Mask

https://codeforces.com/problemset/problem/146/B 

数据很小,暴力即可

#include <bits/stdc++.h>

using namespace std;

int a, b;
string k;
bool check(int s)
{
    string w;
    int tmp = s;
    while(tmp) {
        w.push_back(tmp%10 + '0');
        tmp /= 10;
    }
    reverse(w.begin(), w.end());
    int c = 0, l = w.size(), r = k.size();
    string ss;
    for(int i = 0;i < l; ++ i) {
        if((w[i] == '4' || w[i] == '7')) {
            ss.push_back(w[i]);
        }
    }
    if(ss.size() == r && ss == k) return true;
    return false;
}
int main()
{
    cin >> a >> b;
    int tmp = b;
    while(tmp) {
        k.push_back(tmp%10 + '0');
        tmp /= 10;
    }
    reverse(k.begin(), k.end());
    for(int i = a + 1;i <= 10000010; ++ i) {
        if(check(i)){
            cout << i << endl;
            return 0;
        }
    }
//    puts("YES");
}

D. Lucky Number 2

分类讨论...各种情况,对着cf上的输入数据才ac

#include <bits/stdc++.h>

using namespace std;

int a, b, c, d, f;
string s;
int main()
{
    cin >> a >> b >> c >> d; // 4, 7, 47, 74
    if(abs(c - d) > 1) {
        puts("-1");
        return 0;
    }
    if(d > c) f = 1;
    if(c == d) {
        a -= c;
        b -= c;
        if(a < 0 || b < 0) {
            puts("-1");
            return 0;
        }
        if(a - 1 == -1) {
            if(b - 1 == -1) {
                puts("-1");
                return 0;
            }
            while(d) cout << "74", d --;
            while(b) cout << "7", b --;
            return 0;
        }
        a --;
        while(a) cout << '4', a --;
        while(c) cout << "47", c --;
        while(b) cout << "7", b --;
        putchar('4');
        return 0;
    }
    if(f == 0) {
        a -= c; // 47 用去的a, b 
        b -= c;
         d -= (c - 1); // 47 构成的74, c >= 1 
        if(a < 0 || b < 0) {
            puts("-1");
            return 0;
        }
        // cnt(47) >= cnt(74);
        while(a) putchar('4'), a --;
        while(c) cout << "47", c --;
        if(d) {
            while(b) putchar('7'), b --;
            putchar('4');
        } else {
            while(b) putchar('7'), b --;
        }
    } else {
    //    cnt(74) - cnt(47) == 1;
        a -= d;
        b -= d;
        if(a < 0 || b < 0) {
            puts("-1");
            return 0;
        }
        printf("74"), d --;    
        while(a) printf("4"), a --;
        while(d > 1) printf("74"), d --;
        while(b) printf("7"), b --;
        while(d) printf("74"), d --;
    }
}
//747477777744444
//774744

 

posted @ 2020-12-17 17:35  IIlIlIlI  阅读(59)  评论(0编辑  收藏  举报