3.15 주전 A

링크: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 이 가장 많은 상황 으로 그들의 자릿수 를 비교 했다.무릎 꿇 었 어, 돼지 머리.길이 가 이 두 개 로 합쳐서...

좋은 웹페이지 즐겨찾기