C ++,926字节
#include<iostream>
#include<string>
#include<math.h>
#define S string
using namespace std;S N(S x,int y){S z="";for(int q=0;q<y;q++){z+=x;}return z;}int main(){int n=0,t=0,g=0,fi=1;cin>>n;int t1[]={0,0,n,0};int t2[]={0,n-2,n-2,1};for(int k=0;k<n+1;k++){if((k>(n-2)/2)&&(k<(n+5)/2)){if(g==0){S d,e;if(!((n+1)%4)){cout<<N("* ",t2[0])<<" *"<<N(" *",t2[0])<<endl<<N("* ",(n+1)/2)<<endl<<N("* ",t2[0])<<"***"<<N(" *",t2[0])<<endl;t2[2]=n-8-(n-11);t1[2]=n-4-(n-11);t1[0]--;t2[3]--;t1[3]-=2;}else{cout<<N("* ",t1[0])<<"***"<<N(" *",t2[0])<<endl<<N("* ",(n+1)/2)<<endl<<N("* ",t1[0])<<"* "<<N(" *",t2[0])<<endl;t2[0]--;t1[2]+=2;t2[2]+=6;t1[3]--;t2[1]-=2;t2[3]-=2;}fi=0;}g=5;}else{t=1-t;int*tR;tR=t?t1:t2;cout<<N("* ",tR[0])<<N(t?"*":" ",tR[2])<<N(" *",tR[3])<<endl;if(fi){if(t){t1[0]+=k==0?0:1;t1[2]-=k==0?2:4;t1[3]++;}else{t2[0]++;t2[2]-=4;t2[3]++;}}else{if(t){t1[0]--;t1[2]+=4;t1[3]--;}else{t2[0]--;t2[2]+=4;t2[3]--;}}}}return 0;}
这不是很优雅,但是对于大n来说并不会占用太多内存。此外,(几乎可以肯定)大约有20个角色可以进一步打高尔夫球,但我不能忍受了。
简短说明:
这将螺旋线分成两种类型:中间是******的那些,中间是\ s \ s \ s \ s \ s的那些。然后很明显,每一行都由几个“ *”,中间和一些“ *”组成。如果您对模式的观察时间足够长,那么弄清楚每件事到底有多少是简单的。棘手的事情是打印螺旋的中心,我基本上使用条件代码对其进行硬编码。最终这很有用,因为***和\ s \ s \ s行切换为奇/偶。
测试:
输入:( 55
我认为大的看起来最酷)
输出:
****************************************************** *****
*
****************************************************** *** *
* * *
* ***************************************************** * *
* * * * *
* * ************************************************* * * *
* * * * * * * *
* * * ********************************************* * * * *
* * * * * * * * * *
* * * * ************************************* * * * * *
* * * * * * * * * * * *
* * * * * ********************************* * * * * * *
* * * * * * * * * * * * * *
* * * * * * ***************************** * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * ************************* * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ********************* * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * ***************** * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * ************* * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * ********* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * ***** * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * {-我的程序在这里添加了一个空格
* * * * * * * * * * * * * *** * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * ***** * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * *********** * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * *************** * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * ******************* * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * *********************** * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * *************************** * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ******************************* * * * * * *
* * * * * * * * * * * * *
* * * * * *********************************** * * * * *
* * * * * * * * * * *
* * * * ******************************************* * * * *
* * * * * * * * *
* * * *********************************************** * * *
* * * * * *
* * *************************************************** * *
* * * *
* ***************************************************** ** *
* *
****************************************************** *****
输入: 3
输出:
***
*
* *
***
注意:我不是计算机科学家/计算机科学专业的学生,并且我不知道如何证明它使用了O(log n)内存。我只能根据问题中的链接来确定要做什么。如果有人可以确认/否认此答案是否有效,我将不胜感激。我对这个答案的有效性的逻辑是,除了输入本身之外,它从不存储任何基于n的大小变量。相反,运行n次的for循环会根据n计算整数值。无论输入内容如何,这些值的数量均相同。
注意2:由于我处理中间值的方法,因此不适用于n = 1。这很容易用条件语句解决,因此,如果有人在我的答案的几个字符范围内,我会解决它;)
在ideone上玩。
n
在O(1)存储器中。