[英]Print a triangle in C
I want to develop a program which prints a triangle shown below: 我想开发一个打印三角形的程序,如下所示:
1
A B
1 2 3
A B C D
Using a for loop in C. Any idea how come up with program? 在C语言中使用for循环。您知道如何编写程序吗?
If you want to print n
lines in total: 如果要总共打印
n
行:
n-1
spaces in front of it n-1
空格 n-2
spaces in front of them n-2
空格 n-3
spaces in front of them n-3
空格 i
-th line consists of ___
chars and ___
spaces in front of them (please fill in missing fields i
行由___
字符和___
前面的空格组成(请填写缺少的字段 How to determine what to print: 如何确定要打印的内容:
Please formulate a rule that determines which lines contain which signs: 请制定规则,确定哪些行包含哪些符号:
_______________________________________________________________________________
_______________________________________________________________________________
You can print numbers with: printf("%d", number)
. 您可以使用以下命令打印数字:
printf("%d", number)
。 You can print chars with printf("%c",char)
. 您可以使用
printf("%c",char)
打印字符。
You can do addition on characters as well: 'A' + 2
yields 'C'
. 您还可以对字符进行加法:
'A' + 2
产生'C'
。
Now it should be no real problem to program the program you are looking for. 现在,对要寻找的程序进行编程应该不是真正的问题。
char * pie = " 1\n A B\n 1 2 3\n A B C D\n";
for (i=0;i<1;i++) printf("%s", pie);
f(int n)
{
int i , j ;
for( i = 1 ; i <= n ; i ++ )
{
j = 1 ;
while( j <= (n-i) ) { printf(" "); j++ ;}
j = 0 ;
while( j <= i )
{
if( i % 2 != 0 )
printf("%d ", j );
else
printf("%c ", j + 'A' );
printf("\n");
j ++ ;
}
}
}
Now if someone can do it in time < O(n2) That is welcome :) 现在,如果有人可以及时做到这一点<O(n2)那就好了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.