것부터 먼저 외측 루프 제어 변수 d
을 사용하고 제 2 내부 루프 재 세트 그것을 1
행 :
가
for (d = 1 ; d <= n ; d++)
:
for (d = 1 ; c <= d ; c++)
내부 루프이어야 d
대신 c
을 사용하십시오. 당신이 것을 해결되면, 당신은 조금 더 가까이 뭔가를 얻을 :
11
2332
345543
45677654
5678998765
당신은에서 볼 수
중간 자리는 너무 많은 시간을 인쇄되는 세 번째 내부 루프를 조정하여 일을 시작하는 것을 당신은 해결할 수 낮은 값 (세 개의 내부 루프가 선행 공백위한 명심 카운트 업 카운트 다운) :
num--; // one value lower
for (c = 1 ; c < d ; c++) // one less time (< instead of <=)
그 변화와 함께, 당신은 마침내 바로 모든 숫자를 가지고 있지만 간격은 약간의 작업이 필요합니다.
,451,515,
1
232
34543
4567654
567898765
그리고 그 제 1 내부 루프 조정하는 단순한 문제이다 (사용자 입력이 아닌 정수 5
를 사용하여 임시 변경) 모든 그 변화
for (c = 0 ; c < space ; c++) // start at zero rather than one
을 우리가 끝날
#include <stdio.h>
int main (void) {
int n, c, d, num = 1, space;
n = 5; // scanf ("%d",&n);
space = n - 1;
for (d = 1; d <= n; d++) {
num = d;
for (c = 0; c < space; c++)
printf (" ");
space--;
for (c = 1; c <= d; c++) {
printf ("%d", num);
num++;
}
num--;
num--;
for (c = 1; c < d; c++) {
printf ("%d", num);
num--;
}
printf ("\n");
}
return 0;
}
그리고 우리와 끝까지 : 위로
,249,625 원하는대로
. 여기에서 알 수 있듯이 물론
, 당신은 단지 시작에 대해 생각하면이 일을하고 3 개 개의 내부 루프의 각각의 조건을 끝의 더 간결한 방법이있다 :
#include <stdio.h>
int main (void) {
int numLines;
numLines = 5;
for (int line = 1; line <= numLines; line++) {
// Spaces at start of line.
for (int spaces = 0; spaces < numLines - line; spaces++)
putchar (' ');
// Ascending digits.
for (int digit = line; digit < line * 2; digit++)
printf ("%d", digit);
// Descending digits and newline.
for (int digit = line * 2 - 2; digit >= line; digit--)
printf ("%d", digit);
putchar ('\n');
}
return 0;
}