弗洛伊德三角反转
Floyd's Triangle Reverse
我必须制作一个按以下顺序打印的弗洛伊德三角形:
7 8 9 10
4 5 6
2 3
1
但目前我的代码打印如下:
1
2 3
4 5 6
7 8 9 10
代码:
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a);
a++;
}
printf("\n");
}
return 0;
}
有人可以帮助我吗?
我通过破解解决了它(即尝试不同的东西并根据输出调整代码):
我首先将生成代码移到一个单独的函数中。然后我剪切并粘贴了这个函数,给它起了一个不同的名字。然后我开始修改 for
循环。
我经历了四个版本,直到找到正确的版本:
#include <stdio.h>
#include <stdlib.h>
void
fwd(int n)
{
int i, c;
int a = 1;
for (i = 1; i <= n; i++) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev1(int n)
{
int i, c;
int a = 1;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev2(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", a);
a--;
}
printf("\n");
}
}
void
rev3(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", (c - i) + a);
a--;
}
printf("\n");
}
}
void
rev4(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", (c - i) + a);
}
a -= i;
printf("\n");
}
}
int
main(int argc,char **argv)
{
int n;
--argc;
++argv;
if (argc > 0)
n = atoi(*argv);
else
scanf("%d", &n);
printf("fwd:\n");
fwd(n);
printf("\nrev1:\n");
rev1(n);
printf("\nrev2:\n");
rev2(n);
printf("\nrev3:\n");
rev3(n);
printf("\nrev4:\n");
rev4(n);
return 0;
}
程序输出如下:
fwd:
1
2 3
4 5 6
7 8 9 10
rev1:
1 2 3 4
5 6 7
8 9
10
rev2:
10 9 8 7
6 5 4
3 2
1
rev3:
10 8 6 4
6 4 2
3 1
1
rev4:
7 8 9 10
4 5 6
2 3
1
给你。
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
int width = 0;
for ( unsigned int tmp = n * ( n + 1 ) / 2; tmp != 0; tmp /= 10 )
{
++width;
}
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = ( n - i ) * ( n - i + 1 ) / 2 - ( n - i - 1 );
for ( unsigned int j = 0; j < n - i; j++ )
{
printf( "%*u ", -width, value++ );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
程序输出可能看起来像
Enter a non-negative number (0 - exit): 10
46 47 48 49 50 51 52 53 54 55
37 38 39 40 41 42 43 44 45
29 30 31 32 33 34 35 36
22 23 24 25 26 27 28
16 17 18 19 20 21
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 4
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 0
其他答案很好,但没有人发布递归函数,所以我想我应该添加一个。
#include <stdio.h>
void recursive(int n, int i);
void straight(int n);
// one way of doing it with a recursive function
void recursive(int n, int i) {
if(n <= 0)
return;
if(i <= n) {
printf( "%-3d", (((n * (n + 1) / 2) - n) + i) );
recursive(n, i + 1);
} else {
printf("\n");
recursive(n - 1, 1);
}
}
// straightforward nested loop way
void straight(int n) {
int i, j, row_sum;
for(i = n; i > 0; --i) {
// the sum: i + (i-1) + (i-2) + ... + 2 + 1 = (i * (i+1)) / 2
row_sum = (i * (i + 1)) / 2;
for(j = i; j > 0; --j) {
printf("%-3d", row_sum - j + 1);
}
printf("\n");
}
}
// entry point
int main(int argc, char **argv) {
int n = 0;
scanf("%d", &n);
printf("Recursive Output for n=%d\n", n);
recursive(n, 1);
printf("\nStraight Output for n=%d\n", n);
straight(n);
return 0;
}
输出:
Recursive Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Straight Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
我必须制作一个按以下顺序打印的弗洛伊德三角形:
7 8 9 10
4 5 6
2 3
1
但目前我的代码打印如下:
1
2 3
4 5 6
7 8 9 10
代码:
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a);
a++;
}
printf("\n");
}
return 0;
}
有人可以帮助我吗?
我通过破解解决了它(即尝试不同的东西并根据输出调整代码):
我首先将生成代码移到一个单独的函数中。然后我剪切并粘贴了这个函数,给它起了一个不同的名字。然后我开始修改 for
循环。
我经历了四个版本,直到找到正确的版本:
#include <stdio.h>
#include <stdlib.h>
void
fwd(int n)
{
int i, c;
int a = 1;
for (i = 1; i <= n; i++) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev1(int n)
{
int i, c;
int a = 1;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev2(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", a);
a--;
}
printf("\n");
}
}
void
rev3(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", (c - i) + a);
a--;
}
printf("\n");
}
}
void
rev4(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", (c - i) + a);
}
a -= i;
printf("\n");
}
}
int
main(int argc,char **argv)
{
int n;
--argc;
++argv;
if (argc > 0)
n = atoi(*argv);
else
scanf("%d", &n);
printf("fwd:\n");
fwd(n);
printf("\nrev1:\n");
rev1(n);
printf("\nrev2:\n");
rev2(n);
printf("\nrev3:\n");
rev3(n);
printf("\nrev4:\n");
rev4(n);
return 0;
}
程序输出如下:
fwd:
1
2 3
4 5 6
7 8 9 10
rev1:
1 2 3 4
5 6 7
8 9
10
rev2:
10 9 8 7
6 5 4
3 2
1
rev3:
10 8 6 4
6 4 2
3 1
1
rev4:
7 8 9 10
4 5 6
2 3
1
给你。
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
int width = 0;
for ( unsigned int tmp = n * ( n + 1 ) / 2; tmp != 0; tmp /= 10 )
{
++width;
}
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = ( n - i ) * ( n - i + 1 ) / 2 - ( n - i - 1 );
for ( unsigned int j = 0; j < n - i; j++ )
{
printf( "%*u ", -width, value++ );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
程序输出可能看起来像
Enter a non-negative number (0 - exit): 10
46 47 48 49 50 51 52 53 54 55
37 38 39 40 41 42 43 44 45
29 30 31 32 33 34 35 36
22 23 24 25 26 27 28
16 17 18 19 20 21
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 4
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 0
其他答案很好,但没有人发布递归函数,所以我想我应该添加一个。
#include <stdio.h>
void recursive(int n, int i);
void straight(int n);
// one way of doing it with a recursive function
void recursive(int n, int i) {
if(n <= 0)
return;
if(i <= n) {
printf( "%-3d", (((n * (n + 1) / 2) - n) + i) );
recursive(n, i + 1);
} else {
printf("\n");
recursive(n - 1, 1);
}
}
// straightforward nested loop way
void straight(int n) {
int i, j, row_sum;
for(i = n; i > 0; --i) {
// the sum: i + (i-1) + (i-2) + ... + 2 + 1 = (i * (i+1)) / 2
row_sum = (i * (i + 1)) / 2;
for(j = i; j > 0; --j) {
printf("%-3d", row_sum - j + 1);
}
printf("\n");
}
}
// entry point
int main(int argc, char **argv) {
int n = 0;
scanf("%d", &n);
printf("Recursive Output for n=%d\n", n);
recursive(n, 1);
printf("\nStraight Output for n=%d\n", n);
straight(n);
return 0;
}
输出:
Recursive Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Straight Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1