链接:http://codeforces.com/problemset/problem/109/A
比赛时写的是在太复杂我都不忍直视了。。
其实就是应该想到数字最小的话首先就是位数最少,然后让4在前面7在后面就可以了,位数最小的话只要让7最多就行了,这样位数一定最小。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 1e9
using namespace std;
int main ()
{
int n;
while(cin >> n && n>0)
{
int flag = 1;
int c = n / 7;//最多可以有几个7
int x,y,x1,y1;
for(int i = c;i >= 0;i--) //逆序到0,看是否可以满足条件 这样的话一定是最优解。
{
int temp = n-i*7;
if(temp%4==0 && temp >= 0)
{
flag = 0;
x1 = temp /4;
y1 = i;
break;
}
}
if(flag)
cout << -1;
else
{
for(int i = 0;i < x1;i++)
cout << 4;
for(int i = 0;i < y1;i++)
cout << 7;
}
cout << endl;
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 1e9
using namespace std;
int main ()
{
int n;
while(cin >> n && n>0)
{
int flag = 1;
int a = n / 4;
int x,y,x1,y1;
for(int i = 0;i <= a;i++) //从0开始到a正序可以使得7的位数最多,这样就能保证总的位数最少。(并且对于一个可能结果最多的位数不会超过a+1)
{ //这样就能直接找到最优解,而不需要找出4最多的情况和7最多的情况。。。。
int temp = n - i*4;
if(temp%7==0 && temp >= 0)
{
flag = 0;
x = i;
y = temp / 7;
break;
}
}
if (flag)
cout << -1;
else
{
for(int i = 0;i < x;i++)
cout << 4;
for(int i = 0;i < y;i++)
cout << 7;
}
cout << endl;
}
return 0;
}
这两个想法都是一样的,最脑残的是比赛的时候我还分了两种情况,4最多的情况,7最多的情况,来比较他们的位数。。。简直跪了猪脑子orz。。就长度变成这两个合起来···