济南营销型网站建设贵吗营销网站制作平台有哪些
news/
2025/10/8 1:17:28/
文章来源:
济南营销型网站建设贵吗,营销网站制作平台有哪些,早期经典网页游戏,网站建设现在市场大不大谢宾斯基三角形是一个有意思的图形#xff0c;#xff08;英语#xff1a;Sierpinski triangle#xff09;是一种分形#xff0c;由波兰数学家谢尔宾斯基在1915年提出,它是一种典型的自相似集。先画一个三角形#xff0c;然后呢#xff0c;取三角形的中点#xff0c;组… 谢宾斯基三角形是一个有意思的图形英语Sierpinski triangle是一种分形由波兰数学家谢尔宾斯基在1915年提出,它是一种典型的自相似集。先画一个三角形然后呢取三角形的中点组成一个新的三角形把新的三角形挖空。依次递归就出现了后面的那个图形。如果用C语言来画一个这样的三角形我们需要怎么画呢我们先看看这样一段代码思路还是跟之前一样在屏幕上画出一个矩形x行和y列。#include stdio.h
#include time.h
#include sys/select.h#define SIZE (1 5)/*64*//* 毫秒级 延时 */
void msleep(int ms)
{struct timeval delay;delay.tv_sec 0;delay.tv_usec ms * 1000; // 20 msselect(0, NULL, NULL, NULL, delay);
}int main()
{int x, y, i;printf(%d\n,SIZE);/*y用来控制列数*/for (y SIZE - 1; y 0; y--, msleep(20),putchar(\n)) {/*控制行输出*/for (i 0; i y; i) {msleep(20);putchar(^);}}return 0;
}
代码输出为了方便大家观看我做了一些调整为了测试我把代码改成这样方便大家看到输出。#include stdio.h#define SIZE (1 3)
int main()
{int x, y, i;printf(%d\n,SIZE);/*y用来控制列数*/for (y SIZE - 1; y 0; y--,putchar(\n)) {/*控制行输出*/for (i 0; i y; i) {putchar(^);}for (x 0; x y SIZE; x){putchar(#);}}return 0;
}
代码输出weiqifabsp-ubuntu1804:~/c$ gcc shengdanshu.c ./a.out
8
^^^^^^^#
^^^^^^##
^^^^^###
^^^^####
^^^#####
^^######
^#######
########
weiqifabsp-ubuntu1804:~/c$这里可以好好分析一下y 长度是用来控制输出多少行可以看到一共有 8 行。i 的长度是用来输出 ^ 字符的这个字符随着 y的减少也会相应减小。x 也受到y 的限制主要是在另一半输出 # 号字符。知道了上面我们来看看核心代码#include stdio.h#define SIZE (1 3)
int main()
{int x, y, i;printf(%d\n,SIZE);/*y用来控制列数*/for (y SIZE - 1; y 0; y--,putchar(\n)) {/*控制行输出*/for (i 0; i y; i) {putchar(^);}for (x 0; x y SIZE; x){printf((x y) ? : *);}}return 0;
}
代码输出8
^^^^^^^*
^^^^^^**
^^^^^* *
^^^^****
^^^* *
^^** **
^* * * *
********
已经有了我们题目上所的三角形的模样了这里只要再稍微修改下就可以得到我们题目中所的那样的三角形了。不对称的原因主要是因为字符高度是宽度的两倍。代码修改成这样#include stdio.h#define SIZE (1 3)
int main()
{int x, y, i;printf(%d\n,SIZE);/*y用来控制列数*/for (y SIZE - 1; y 0; y--,putchar(\n)) {/*控制行输出*/for (i 0; i y; i) {putchar(^);}for (x 0; x y SIZE; x){printf((x y) ? : * );}}return 0;
}
代码输出weiqifabsp-ubuntu1804:~/c$ gcc shengdanshu.c ./a.out
8
^^^^^^^*
^^^^^^* *
^^^^^* *
^^^^* * * *
^^^* *
^^* * * *
^* * * *
* * * * * * * *
weiqifabsp-ubuntu1804:~/c$
然后我们把 ^ 字符替换成空格也就是我们想要的东西了。然后空格和 * 的字符输出主要是靠 x y 来控制的他们又是如何控制的呢我们计算一下上面的算法绿色的地方是我们输出 * 字符的位置蓝色的 是我们输出 空格的位置空格是两个空格所以就出现了我们看到的那样。我们再修改下代码#include stdio.h#define SIZE (1 5)
int main()
{int x, y, i;printf(%d\n,SIZE);/*y用来控制列数*/for (y SIZE - 1; y 0; y--,putchar(\n)) {/*控制行输出*/for (i 0; i y; i) {putchar( );}for (x 0; x y SIZE; x){printf((x y) ? : * );}}return 0;
}
代码输出weiqifabsp-ubuntu1804:~/c$ gcc shengdanshu.c ./a.out
32** ** ** * * ** ** * * ** * * ** * * * * * * ** ** * * ** * * ** * * * * * * ** * * ** * * * * * * ** * * * * * * ** * * * * * * * * * * * * * * ** ** * * ** * * ** * * * * * * ** * * ** * * * * * * ** * * * * * * ** * * * * * * * * * * * * * * ** * * ** * * * * * * ** * * * * * * ** * * * * * * * * * * * * * * ** * * * * * * ** * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
weiqifabsp-ubuntu1804:~/c$
这样看起来是不是很酷了。我在我的另一个号里面用这样方法画了一个圣诞树我觉得也挺有意思的喜欢的同学可以看看当时写那个代码的时候是圣诞夜我们刚好在开会觉得有点无聊。链接如下如何用 C 语言画一个「圣诞树」知乎上的大神画圣诞树基础理论也是基于这个后续剖析一下我觉得非常有意思。附上几张谢宾斯基三角形的图片参考[1]https://www.cnblogs.com/lfri/p/10128073.html[2]https://codegolf.stackexchange.com/questions/6281/draw-a-sierpinski-triangle/6292#6292推荐阅读专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/931020.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!