C++基础题 | 专栏 74 心形图案
source link: https://blog.51cto.com/u_15681752/6870518
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C++基础题 | 专栏 74 心形图案
精选 原创常驻开头:如果你已经有编程经验,请忽略这个贴,基础题对你起不了太多帮助,直接刷力扣就好啦。
这些题以大学生最常用的一本书谭浩强《C语言程序设计》为主,如果你是应付期末考试、专升本,符合你的需求,因为这本书就是很多考试的标准。
例74:C语言实现打印出心形,初学者的表白神器。
解题思路:这道例题我分了4部分,前3行一部分,4-6行一部分,7-13行一部分,最后一行一部分,读者请仔细阅读注释,小林写的很详细了。
前三行输出,为了让初学者知道,即使最笨的方法也是可以打印的。
4-6行,这三行输出效果一样。
for(i=0;i<3;i++)
for(j=0;j<29;j++)
printf("*");
printf("\n");
最后一行,只是输出一个*。
for(i=0;i<14;i++)
printf(" ");
printf("*\n");
C语言源代码演示:
#include<stdio.h>//头文件
int main()//主函数入口
printf(" **** ****\n");
printf(" ********* *********\n");
printf("************* *************\n");
int i,j;
for(i=0;i<3;i++)
for(j=0;j<29;j++)
printf("*");
printf("\n");
for(i=0;i<7;i++)
for(j=0;j<2*(i+1)-1;j++)
printf(" ");
for(j=0;j<27-i*4;j++)
printf("*");
printf("\n");
for(i=0;i<14;i++)
printf(" ");
printf("*\n") ;
return 0;
编译运行结果如下:
C++源代码演示:
#include <iostream>
#include<cstdio>
using namespace std;
int main()//主函数入口
cout<<" **** ****\n";
cout<<" ********* *********\n";
cout<<"************* *************\n";
int i,j;
for(i=0;i<3;i++)
for(j=0;j<29;j++)
cout<<"*";
cout<<"\n";
for(i=0;i<7;i++)
for(j=0;j<2*(i+1)-1;j++)
cout<<" ";
for(j=0;j<27-i*4;j++)
cout<<"*";
cout<<"\n";
for(i=0;i<14;i++)
cout<<" ";
cout<<"*\n";
return 0;
编译运行结果:
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK