PKU 2083 Fractal

Source: Internet
Author: User

I don't need to say much about the question.

Solution:

Since N is not large and the maximum character is 729*729 (3 ^ 6), you can use a two-dimensional array to store the characters that should be printed in each column of each line. Recursively assign the corresponding position to 'x '. The size of each recursion is the upper-layer size/3 until size = 1; each recursion transmits the corresponding coordinates.

 

Code:

# Include <cstdio> <br/> # include <cstring> <br/> # include <cmath> <br/> # define size 731 </P> <p> int getpow (int n) <br/>{< br/> return (INT) Pow (3, (double) N ); <br/>}</P> <p> char list [size] [size]; </P> <p> void FN (INT size, int X, int y) <br/>{< br/> If (size = 1) <br/>{< br/> list [x] [Y] = 'X '; <br/>}< br/> else <br/> {<br/> int d = size/3; <br/> FN (size/3, X, y); <br/> FN (size/3, X, Y + 2 * D); <br/> FN (size/3, X + D, Y + d); <br/> FN (size/3, x + 2 * D, Y); <br/> FN (size/3, X + 2 * D, Y + 2 * D); <br/>}</P> <p> int main () <br/>{< br/> int N; <br/> int size; <br/> while (scanf ("% d", & N) & n! =-1) <br/>{< br/> memset (list, 0, sizeof (list); <br/> size = getpow (n-1 ); <br/> FN (size, 0, 0); </P> <p> for (INT I = 0; I <size; ++ I) <br/>{< br/> for (Int J = 0; j <size; ++ J) <br/>{< br/> printf ("% C ", list [I] [J] = 'X '? 'X': ''); <br/>}< br/> printf ("/N "); <br/>}< br/> printf ("-/N"); <br/>}< br/> return 0; <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.