lucky sum of digit

A. Lucky Sum of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with the task.

<p< p="" style="color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 21px; "><p< p="">
Input
<p< p="">

The single line contains an integer n (1 ≤ n ≤ 106) — the sum of digits of the required lucky number.

<p< p=""><p< p="">
Output
<p< p="">

Print on the single line the result — the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1.

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
11
output
47
input
10
output
-1
 
   
一开始没想清楚,乱打bfs和dfs,后面想了下 发现DFS和BFS明显不行。。随便一下就会爆掉。
其实构成n的数只有4,7那么直接把最大的4,和7的个数求出来。然后在这里面暴力就可以解决了。然而最小的数肯定是把4放在最前面。
 
   
下面是AC代码:
#include<iostream>
#include<string>
#include<queue>
using namespace std;
int n,i,j,k,flag,sum;
int main()
{
	int s4,s7;
	cin>>n;
	s4=n/4; s7=n/7;
	for(i=0;i<=s4;i++)
	{
		for(j=0;j<=s7;j++)
		{
			if(i*4+j*7==n)
			{
				flag=1;
				break;
			}
		}
		if(flag)  break;
	}
	if(!flag)
		cout<<-1<<endl;
	else
	{
		for(k=1;k<=i;k++)
			printf("4");
		for(k=1;k<=j;k++)
			printf("7");
		printf("\n");
	}

	return 0;
}


 
   
 
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值