POJ 2876 Cantoring Along

Description

The Cantor set was discovered by Georg Cantor. It is one of the simpler fractals. It is the result of an infinite process, so for this program, printing an approximation of the whole set is enough. The following steps describe one way of obtaining the desired output for a given order Cantor set:
  1. Start with a string of dashes, with length 3order
  2. Replace the middle third of the line of dashes with spaces. You are left with two lines of dashes at each end of the original string.
  3. Replace the middle third of each line of dashes with spaces. Repeat until the lines consist of a single dash.
For example, if the order of approximation is 3, start with a string of 27 dashes:
---------------------------
Remove the middle third of the string:
---------         ---------
and remove the middle third of each piece:
---   ---         ---   ---
and again:
- -   - -         - -   - -
The process stops here, when the groups of dashes are all of length 1. You should not print the intermediate steps in your program. Only the final result, given by the last line above, should be displayed.  

Input

Each line of input will be a single number between 0 and 12, inclusive, indicating the order of the approximation. The input stops when end-of-file is reached.

Output

You must output the approximation of the Cantor set, followed by a newline. There is no whitespace before or after your Cantor set approximation. The only characters that should appear on your line are '-' and ' '. Each set is followed by a newline, but there should be no extra newlines in your output.

Sample Input

0
1
3
2

Sample Output

-
- -
- -   - -         - -   - -
- -   - -

【思路】:递归题,用时稍久,希望你写出用时更短的代码。我们可以将一个数组赋值为空格,然后对该数组进行分割,同样满足题意,每次丢掉中间的三分之一,然后递归循环此过程,将其划分的更小,直到分割到长度为1,结束返回。。
#include <iostream>
#include <cmath>
using namespace std;

void along(int Along)
{
    if(Along<3)
    {
        cout<<'-';
    }
    else
    {
        if(Along>=3)
            along(Along/3);
        for(int i=1;i<=Along/3;i++)//这儿应该是  Along/3  假如是你之前的话  打印较多的空格  你仔细看一下 
            cout<<" ";
        if(Along>=3)
            along(Along/3);    
    }
}

int main()
{
    int n;
    while(cin>>n)
    { 
        int Along=(int)pow(3.0,n);
        along(Along);
        cout<<endl;
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/wft1990/p/6055916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值