NC50952. Fractal
描述
B(n - 1) B(n - 1) B(n - 1) B(n - 1) B(n - 1)
输入描述
The input consists of several test cases. Each line of the input contains a positive integer n which is no greater than 7. The last line of input is a negative integer −1 indicating the end of input.
输出描述
For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case.
示例1
输入:
1 2 3 4 -1
输出:
X - X X X X X - X X X X X X X X X X X X X X X X X X X X X X X X X - X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X -
C++14(g++5.4) 解法, 执行用时: 314ms, 内存消耗: 8808K, 提交时间: 2020-02-08 21:36:29
#include<iostream> #include<bitset> using namespace std; bitset<1000> map[1000]; void solve(int x,int y,int n){ if(n==1) { map[x].set(y); return; } int t=1; for(int i=1;i<n-1;i++) t*=3; solve(x,y,n-1); solve(x,y+2*t,n-1); solve(x+t,y+t,n-1); solve(x+2*t,y,n-1); solve(x+2*t,y+2*t,n-1); } int main(){ int n; while(cin>>n&&n!=-1){ solve(1,1,n); int t=1; for(int i=0;i<n-1;i++) t*=3; for(int i=1;i<=t;i++){ for(int j=1;j<=t;j++) { if(map[i][j]) cout<<'X'; else cout<<' '; } cout<<endl; } cout<<'-'<<endl; } }
C++(g++ 7.5.0) 解法, 执行用时: 513ms, 内存消耗: 17420K, 提交时间: 2023-07-16 17:04:48
#include<bits/stdc++.h> using namespace std; char s[3001][3001]; inline void dfs(long long x){ if(x==1){s[1][1]='X';return;} dfs(x-1); long long l=pow(3,x-2); for(register int i=1;i<=l;++i) for(register int j=1;j<=l;++j)s[i+2*l][j]=s[i+l][j+l]=s[i][j+2*l]=s[i+2*l][j+2*l]=s[i][j]; } int main(){ long long n; for(register int i=0;i<3001;++i) for(register int j=0;j<3001;++j)s[i][j]=' '; while(scanf("%lld",&n)&&~n){ dfs(n); for(register int i=1;i<=pow(3,n-1);++i){ for(register int j=1;j<=pow(3,n-1);++j)putchar(s[i][j]); puts(""); } puts("-"); } }
C++ 解法, 执行用时: 12ms, 内存消耗: 9912K, 提交时间: 2022-07-26 17:22:20
#include <bits/stdc++.h> using namespace std; char a[1111][1111]; void dfs(int num,int i,int j) { if(num==1) { a[i][j]='X'; return; } int x=pow(3,num-2); dfs(num-1,i,j); dfs(num-1,i,j+2*x); dfs(num-1,i+x,j+x); dfs(num-1,i+2*x,j); dfs(num-1,i+2*x,j+x*2); } int n; int main() { memset(a,' ',sizeof(a)); dfs(7,1,1); while(1) { cin>>n; if(n==-1)return 0; int x=pow(3,n-1)+1; for(int i=1; i<x; i++) { a[i][x]='\0'; puts(a[i]+1); a[i][x]=' '; } cout<<'-'<<endl; } }