数字的空洞 水 南邮NOJ 1071

                                                                            数字的空洞

时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 209            测试通过 : 120 

题目描述

在个位数中:04689有一个共同的特征:数形上存在空洞,其中8有两个相切的空洞。 一个非负整数具有多个空洞,给定一个空洞数目h0 ≤ h ≤ 510),请你写一个计算机程序来找出能产生这些空洞的数,要求数应尽可能小,且无前导零。



输入

一行输入一个非负整数h,表示空洞的数目。

输出

能产生这些空洞的最小数。

 

注意:输出部分的结尾要求包含一个多余的空行。

样例输入

0
1
15
70

样例输出

1
0
48888888
88888888888888888888888888888888888

题目来源

“IBM南邮杯”个人赛2009


思路还是很清晰的,特别情况就是h=1和h=0的情况,其他都可以用统一的方法处理,6是用不到的。实现代码如下:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=510+10;
char a[N];
int h;
int main()
{
    while(scanf("%d",&h)==1)
    {
        int cnt=0;
        if(h==0)
        {
            printf("%d
",1);
        }
        if(h==1)
        {
            printf("%d
",0);
        }
        if(h>=2)
        {
            int p=h/2;
            for(int i=1;i<=p;i++)
            {
                a[cnt++]='8';
            }
            h=h-p*2;
            if(h==1)
            {
                a[cnt++]='4';
            }
            for(int j=cnt-1;j>=0;j--)
            {
                printf("%c",a[j]);
            }
            printf("
");
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Tobyuyu/p/4965599.html