原题链接
题意:
一个数字各个位由若干个4和若干个7组成,并且“4”的个数为a1,“7”的个数为a2,“47”的个数是a3,“74”的个数是a4。
我们要做的就是找到这个数,找不到就输出-1;
思路:
1.a3和a4不能比a1和a2大,(a3-a4)的绝对值不能超过1,因为数字只有4和7,格式为··47··47··47··或··74··74··74··,所以不会超过1;
2.当a3-a4 = 1,应为··474747··,剩下的“4”都应该在前面,“7”都应该在后面;
3.当a3 = a4,应为4·474747··74··;
4.当a3-a4 = -1,应为74747··474··;
代码:
#include<bits/stdc++.h>
#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define ull unsigned long long
#define pf printf
#define sf scanf
#define sfd(n) scanf("%d", &n)
#define sfc(n) scanf("%c", &n)
#define sflf(n) scanf("%lf", &n)
#define sfll(n) scanf("%lld", &n)
#define pfd(n) printf("%d", n)
#define pfc(n) printf("%c", n)
#define pflf(n) printf("%lf", n)
#define pfll(n) printf("%lld", n)
#define pft printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define PI 3.1415926
#define MAX 200010
#define MOD 998244353
using namespace std;
int main() {
// FILE *fin, *fout;
// fout = fopen("data.out", "wb");
// int cnt = 0, ans = 0;
// for( int i=2; i<=MAX; i++ ) {
// if( F(i) ) {
// fprintf(fout, "%d, ", i);
// cnt++;
// }
//
// }
// pfd(cnt);
int a, b, c, d;
cin >> a >> b >> c >> d;
if( c>a||c>b||d>a||d>b||abs(c-d)>1 ) {
pfd(-1);
}else {
a -= c;
b -= c;
d -= (c-1);
if( d==2 ) {
if( !(a<0||b<=0) ) {
pfd(7);
b--;
while( (a--)>1 ) pfd(4);
while( c-- ) pfd(47);
while( b-- ) pfd(7);
pfd(4);
}else {
pfd(-1);
}
}else if( d==1 ) {
if( a ) {
while( (a--)>1 ) pfd(4);
while( c-- ) pfd(47);
while( b-- ) pfd(7);
pfd(4);
}else {
if( b ) {
pfd(7);
b--;
while( c-- ) pfd(47);
while( b-- ) pfd(7);
}else {
pfd(-1);
}
}
}else {
while( a-- ) pfd(4);
while( c-- ) pfd(47);
while( b-- ) pfd(7);
}
}
return 0;
}
反思:
wa了很多次的原因有两个,一是之前没注意到数据范围,使用string记录结果,太长了就没过,二是没有细心思考有遗漏的情况。