対称的な二重三角形のパターンをC言語で印刷する
行数を指定すると、プログラムは最も複雑でない対称の二重三角形パターンを印刷する必要があります。
例
Input: 5 Output: 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行です
アルゴリズム
START
STEP 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>
// printing alternate x o starting with x
int printx(int n) {
int i;
for ( i = 1; i < = n; i++) {
if (i % 2! = 0)
printf("x ");
else
printf("o ");
}
return 0;
}
// printing alternate x o starting with o
int printo(int n) {
int i;
for ( i = 1; i < = n; i++) {
if (i % 2! = 0)
printf("o ");
else
printf("x ");
}
return 0;
}
// To print the pattern for n
int printpattern(int n) {
// upper half
// n-1 lines for odd, n-2 lines for even
int x = n;
int i, j;
if (n % 2 == 0)
x = x - 1;
// number of spaces to leave in each line
int p = n - 1;
// number of characters in each line
int s = 1;
// prints double lines in each iteration
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++;
}
// extra upper middle for even
if (n % 2 == 0) {
for ( i = 1; i < = p; i++)
printf(" ");
if (n % 4! = 0)
printx(n / 2);
else
printo(n / 2);
printf("\n");
}
// middle line
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");
// extra lower middle for even
if (n % 2 == 0) {
printf(" ");
printx(n / 2);
printf("\n");
}
// lower half
p = 1;
if (n % 2 == 0) {
x--;
p = 2;
}
int q = x / 2;
// one line for each iteration
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;
} 出力
上記のプログラムを実行すると、次の出力が生成されます。
X
X
O X
O X
X O X O X
X O
X O
X
X -
Cプログラムで行列の対角パターンで数値を印刷します。
タスクは、対角パターンのnxnの行列を印刷することです。 nが3の場合、対角パターンで行列を印刷するのは-です。 したがって、出力は次のようになります- 例 Input: 3 Output: 1 2 4 3 5 7 6 8 9 Input: 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16 この問題は、数値nを与え、n x nの行列を生成
-
Pythonでパターンを印刷する方法は?
Pythonのパターンは、ネストされたforループを使用して印刷できます。外側のループは行数を反復処理するために使用され、内側のループは列数を処理するために使用されます。印刷ステートメントは、要件に応じてさまざまなパターンを形成するように変更されます。 パターンには、星型、数字型、アルファベット型があります。パターンは、さまざまな形、三角形、ピラミッドなどにすることができます。 例 これらのパターンはすべて、これらの異なるパターンを形成する変更されたprintステートメントを含むforループを使用して印刷できます。 これらのパターンの印刷の基本的な考え方は同じですが、わずかな違いが