B. A Prosperous Lot
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.
Big Banban is hesitating over the amount of money to give out. He considers loops to be lucky since it symbolizes unity and harmony.
He would like to find a positive integer n not greater than 1018, such that there are exactly k loops in the decimal representation of n, or determine that such n does not exist.
A loop is a planar area enclosed by lines in the digits’ decimal representation written in Arabic numerals. For example, there is one loop in digit 4, two loops in 8 and no loops in 5. Refer to the figure below for all exact forms.
Input
The first and only line contains an integer k (1 ≤ k ≤ 106) — the desired number of loops.
Output
Output an integer — if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positive decimal integer not exceeding 10^18.
Examples
input
2
output
462
input
6
output
8080
题意一开始没看懂,看懂后发现很简单,就是输入一正整数k,输出一有k个环(圈)的数字,一开始以为题目中的loop(环)是数学概念,随后才发觉原来就是说数字的形状里面有多少个圈,比如“0”上面有一个圈,然后“8”里面有两个,然后“9”里面有一个圈,像第一个样例输入里输入的是“2”,结果其实可以有很多种,最简单的就是8了,这题其实我们无需考虑除了“8”和“0”以外的所有数字,因为“8”和“0”便可组成所有的情况,当然你选择别的组合也可以。
不过要注意的坑便是:
1、k不可以大于36,若k大于36,可直接输出”-1”表示不存在,因为输出的数字不大于10^18,所以最多loop的情况便是8888888888888888,一共16个“8”,36个loop,所以k大于36的情况一律不考虑。
2、输出的数字不能为0,即k=1的时候。不能输出“0”,因此你可以选择没有0的组合,就不需要单独考虑k=1的情况。
核心代码:判断k是否为奇数,不是则输出k/2个“8”,是则输出(k-1)/2个“8”后再输出一个“0”即可。
代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
unsigned long long k,i;
cin>>k; getchar();
if(k==1) printf("10\n");
if(k>36) {puts("-1"); return 0;}
if(k!=1){
if(k%2)
{
for(i=1;i<=(k-1)/2;i++)
printf("8");
puts("0");
}
else
{
for(i=1;i<=(k)/2;i++)
printf("8");
cout<<'\n';
}
}
return 0;
}