【CoreForcesラウンド#104D】ラッキーナンバー2

D -ラッキー番号2
イタリアこと:組成物によるラッキー番号4と7は、現在知られている数4、数7、数47及び数74の数を説明します。
この数(最小必須)解決
策:表題構成
\ [ABS(47の数- 74数)> 1 \]
[4,7は以下の数以上で構成されていない\] \
上記の条件は、満たされない場合構成、他の場合には、優先度記憶部4、最小値をとる場合。

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int abs(int a){
    return a<0?-a:a;
}
int main(){
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    if(abs(c-d)>1){
        printf("-1\n");
        return 0;
    }
    string s;
    if(c==d+1){
        for(int i=0;i<c;i++){
            s+="47";
        }
    }
    if(c+1==d){
        for(int i=0;i<d;i++){
            s+="74";
        }
    }
    if(c==d){
        for(int i=0;i<c;i++){
            s+="47";
        }
        if(a>c)s+="4";
        else s="7"+s;
    }
    int ls=0,lt=0;
    for(int i=0;i<s.length();i++){
        if(s[i]=='4')ls++;
        else lt++;
    }
    if(ls>a||lt>b){
        printf("-1\n");
    }
    else{
        int fa=1;
        int na=a-ls,nb=b-lt;
        for(int i=0;i<s.length();i++){
            if(s[i]=='4'&&fa){
                fa=0;
                while(na--){
                    printf("4");
                }
            }
            if(s[i]=='7'){
                lt--;
                if(lt==0){
                    while(nb--){
                        printf("7");
                    }
                }
            }
            printf("%c",s[i]);
        }
    }
    return 0;
}

おすすめ

転載: www.cnblogs.com/Vagrant-ac/p/12151787.html