用C语言打印对称双三角图案
给定行数,程序必须以最小的复杂度打印对称的双三角图案。
示例
Input: 5Output: X
X
O X
O X
X O X O X
X O
X O
X
X
整个问题包含3个不同的分区-
用奇数n的n-1行或偶数n的n-2行打印上半部分。
打印中间行,奇数n为1行,偶数n为3行。
打印下半部分,对奇数n用n-1行,对偶数n用n-2行
算法
STARTSTEP 1: IF (n % 2 == 0) then
x = x - 1;
Define p as n – 1 for spaces
Define s = 1 for characters
STEP 2: LOOP FOR i= 1 AND i <= (x - 1) / 2 AND i++
LOOP FOR j = 1 AND j <= p AND j++
PRINT SPACE
END LOOP
IF i % 2 != 0 then
GOTO STEP 11 Printx(s)
ELSE
GOTO STEP 12 Printo(s)
END
PRINT NEW LINE
INCREMENT p BY 1
LOOP FOR j = 1 AND j <= p AND j++
PRINT SPACE
END LOOP
IF i % 2 != 0 then,
GOTO STEP 11 Printx(s)
ELSE
GOTO STEP 12 Printo(s)
END IF
PRINT NEWLINE
DECREMENT p BY 1
INCREMENT s BY 1
END LOOP
STEP 3: IF n % 2 == 0 {
LOOP FOR i = 1 AND i <= p AND i++
PRINT SPACE
IF n % 4 != 0 then,
GOTO STEP 11 Printx(n/2)
ELSE
GOTO STEP 12 Printo(n/2)
END IF
PRINT SPACE
END IF
STEP 4: IF n % 2 != 0 then,
GOTO STEP
ELSE
IF n % 4 != 0 then,
DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2) and Printx(n/2)
ELSE
DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2) and Printo(n/2)
DIVIDE n BY 2 AND GOTO STEP
END IF
END IF
PRINT NEW LINE
STEP 5: IF n % 2 == 0 then, {
PRINT SPACE
DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2)
PRINT NEWLINE
END IF
STEP 6: SET p = 1
STEP 7: IF n % 2 == 0
DECREMENT x BY 1
SET p = 2
END IF
STEP 8: SET q = x / 2
STEP 9: LOOP FOR i = 1 AND i <= x AND i++) {
LOOP FOR j = 1 AND j <= p AND j++
PRINT SPACE
END FOR
PASS q TO STEP 11 Printx(q)
IF i % 2 == 0 THEN
DECREMENT q BY 1
END IF
PRINT NEWLINE
INCREMENT p BY 1
END FOR
STEP 10: PRINT NEWLINE
STEP 11: Printx(n)
LOOP FOR i = 1 AND i< = n AND i++
IF i % 2 != 0 then,
PRINT x
ELSE
PRINT o
END IF
END FOR
STEP 12: Printo(n)
LOOP FOR i = 1 AND i< = n AND i++
IF i % 2 != 0 then,
PRINT o
ELSE
PRINT x
STOP
示例
#include <stdio.h>//从x开始打印备用xo-
int printx(int n) {
int i;
for ( i = 1; i < = n; i++) {
if (i % 2! = 0)
printf("x ");
else
printf("o ");
}
return 0;
}
//从o开始打印备用xo-
int printo(int n) {
int i;
for ( i = 1; i < = n; i++) {
if (i % 2! = 0)
printf("o ");
else
printf("x ");
}
return 0;
}
//为n打印图案
int printpattern(int n) {
//上半部分
//n-1行表示奇数,n-2行表示偶数
int x = n;
int i, j;
if (n % 2 == 0)
x = x - 1;
//每行中要保留的空格数
int p = n - 1;
//每行字符数
int s = 1;
//在每次迭代中打印双行
for ( i = 1; i < = (x - 1) / 2; i++) {
for ( j = 1; j < = p; j++) {
printf(" ");
}
if (i % 2! = 0)
printx(s);
else
printo(s);
printf("\n");
p++;
for ( j = 1; j < = p; j++)
printf(" ");
if (i % 2! = 0)
printx(s);
else
printo(s);
printf("\n");
p--;
s++;
}
//额外的中上位数
if (n % 2 == 0) {
for ( i = 1; i < = p; i++)
printf(" ");
if (n % 4! = 0)
printx(n / 2);
else
printo(n / 2);
printf("\n");
}
//中线
if (n % 2! = 0)
printx(n);
else {
if (n % 4! = 0) {
printx(n / 2);
printx(n / 2);
} else {
printx(n / 2);
printo(n / 2);
}
}
printf("\n");
//额外的较低中位数
if (n % 2 == 0) {
printf(" ");
printx(n / 2);
printf("\n");
}
//下半部分
p = 1;
if (n % 2 == 0) {
x--;
p = 2;
}
int q = x / 2;
//每次迭代仅一行
for ( i = 1; i < = x; i++) {
for (int j = 1; j < = p; j++)
printf(" ");
printx(q);
if (i % 2 == 0)
q--;
printf("\n");
p++;
}
printf("\n");
return 1;
}
int main() {
int n = 5;
printpattern(n);
return 0;
}
输出结果
如果我们运行上面的程序,那么它将生成以下输出。
XX
O X
O X
X O X O X
X O
X O
X
X
以上是 用C语言打印对称双三角图案 的全部内容, 来源链接: utcz.com/z/316931.html