c - 并排打印两个金字塔(ASCII 图形)
问题描述
我可以破解它直到@symbol,但在那之后就不行了。
我不知道并排实现两个金字塔的逻辑。
int main()
{
int i, space, n, j, l, k=0;
printf("Enter number excluding 1 and -ve numbers: ");
scanf("%d",&n);
for(i=1; i<=n-1; ++i, k=0)
{
for(j=0;j<(n/2);j++)
{
printf("\t");
}
for(space=1; space<=n-i; ++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}//End of upper star pyramid
printf("\n");
}
//logic for @
for(j=1;j<=(n-1);j++)
{
for(l=0;l<(n/2);l++)
{
printf("\t");
}
for(l=1; l<space; ++l)
{
printf(" ");
}
printf("@");
for(l=1; l<=n; l++)
{
printf(" ");
}
printf("@\n");
}
//after that I dont know how to work with two different pyramids side by side
return 0;
}
解决方案
这是一个尝试...
已使用“#define”作为要打印的字符。
使用了三个 do-while 循环来中断打印顶部金字塔、“@”和底部金字塔的任务。
该解决方案适用于提供的示例或用户提供的任何“奇数”数字。
对于偶数,需要研究解决方案(无论哪种方式,金字塔的顶部都不会与偶数的中心对齐)。
希望这会有所帮助.....随意使用代码....
#include <stdio.h>
#define SPACE ' '
#define STAR '*'
#define AT '@'
int main(){
int num = 7;//input from the user, have used 7 as an example
int total_hor_length = num * 3;//# of characters to be printed on each line
int i = num;
int j = 0;
while(i > 0){//calculate the iterations
j++; //required for top and bottom
i -= 2; //Pyramids
}
int first_print = j; //# first loop iteration
int last_print = first_print; //# last loop iteration
int center_print = (num * 2 - 1)-(first_print + last_print);//# center loop
int pos = total_hor_length/2; //position of top of center pyramid
int count = 0;
int k;
i = 0;//re-initialize i for the first loop
//Logic for the Top Pyramid(first loop)
do{
for(k = 0;k < total_hor_length;++k){ //for input "n"
if((k < pos || k > pos+count)) //by the time this loop finishes
printf("%c",SPACE); //pos will be "n-1"
else //and count will be "n+1"
printf("%c",STAR);
}
putchar('\n');
pos--;
count+=2;
i++;
}while( first_print > i);
i = 0;//re-initialize i for the center loop
//Logic for the "@"s (second loop)
do{
for(k = 0;k < total_hor_length;++k){ //utilizing the values of "count"
if((k == count-1 || k == (count-1)+pos)) //and pos procured from the previous
printf("%c",AT); //loop to print the "@"s
else
printf("%c",SPACE);
}
putchar('\n');
i++;
}while(center_print > i);
i = 0;//re-initialize i for the center loop
//Logic for the Bottom Pyramids (Last loop)
do{
for(k = 0;k < total_hor_length;++k){
if(i == 0){
if((k <= pos-i) || (k > pos + num + i && k < total_hor_length - i))
printf("%c",STAR);
else if(k == pos+1||k == pos+num) //printing the line containing
printf("%c",AT); // asterisk and "@" symbols
else
printf("%c",SPACE);
}
else{
if((k >= i && k <= pos-i) || (k > pos + num + i && k < total_hor_length - i))
printf("%c",STAR); //printing the last lines
else
printf("%c",SPACE);
}
}
putchar('\n');
i++;
}while(i < last_print);
}
推荐阅读
- java - Servlet 在 Eclipse 的运行时抛出异常
- php - 使用队列时,Laravel 作业中的 SoapClient 在一段时间后“无法连接到主机”:工作
- java - 选择项目时如何删除BottomnavigationView的标题?// 安卓
- c# - 将列表中的对象转换为字符串
- django - 从应用程序跨站点范围的页脚加载数据
- angular - 有没有一种简单的方法可以在 Angular 中显示日期时间的倒计时?
- html - 如何在提交时以 Angular 7 反应形式应用验证?
- awk - 基于列的前两个字母进行过滤
- r - 如何删除不满足所有条件的所有行(每组)?
- go - 哪些构建约束子句用于区分 32 位和 64 位整数?