如何减少此 C 程序的标记数

How to reduce the number of tokens for this C program

我编写了一个程序,它使用数字(整数)作为输入,并使用 for 循环输出 1 和 0 的模式。例如当输入为 7 时,输出为:

1 
1 1 
1 0 1 
1 0 0 1 
1 0 0 0 1 
1 0 0 0 0 1 
1 1 1 1 1 1 1

源码如下:

#include <stdio.h>
int main()
{
    int rows;
    int i,j;
    scanf("%d", &rows);
    for(i=0;i<rows;i++)
    {
        for(j=0;j<=i;j++)
        {
            if(i==j)
            {
                printf("1 \n");
            }
            else if(j==0)
            {
                printf("1 ");
            }
            else if (i==rows-1)
            {
                printf("1 ");
            }
            else
            {
                printf("0 ");
            }
       }
    }
   return 0;
}

我编译codem时创建的token个数是107个​​,我需要把token个数降低到102个以下(满足测试要求)。

有什么建议吗?

我会这样写

include <sdtio.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;

        putchar( '\n' );

        for (unsigned int i = 0; i < n; i++)
        {
            for (unsigned int j = 0; j < i + 1; j++)
            {
                unsigned int value = i == 0 || i == n - 1 || j == 0 || j == i;
                printf( "%d ", value );
            }
            putchar( '\n' );
        }

        putchar( '\n' );
    }
}

程序输出可能看起来像

Enter a non-negative number (0 - exit): 1

1

Enter a non-negative number (0 - exit): 2

1
1 1

Enter a non-negative number (0 - exit): 3

1
1 1
1 1 1

Enter a non-negative number (0 - exit): 4

1
1 1
1 0 1
1 1 1 1

Enter a non-negative number (0 - exit): 5

1
1 1
1 0 1
1 0 0 1
1 1 1 1 1

Enter a non-negative number (0 - exit): 6

1
1 1
1 0 1
1 0 0 1
1 0 0 0 1
1 1 1 1 1 1

Enter a non-negative number (0 - exit): 7

1
1 1
1 0 1
1 0 0 1
1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1 1

Enter a non-negative number (0 - exit): 0

或者如果要减少代币数量那么

#include <stdio.h>

int main( void )
{
    unsigned n = 0;

    scanf( "%u", &n );

    for (unsigned i = 0; i < n; i++)
    {
        for (unsigned j = 0; j < i + 1; j++)
        {
            unsigned value = i % ( n - 1 ) == 0 || j % i == 0;
            printf( "%d ", value );
        }
        putchar( '\n' );
    }
}

为什么不去掉 if-else 链中的大括号?那是 8 个代币,这将使您低于您的要求。

检查这个:

int main(void)
{
    int rows;
    scanf("%d", &rows);
    for(int i=0;i<rows;i++)
        for(int j=0;j<=i;j++)
            printf(i == j ? "1 \n" : !j || i == rows - 1 ? "1 " : "0 ");
}

https://godbolt.org/z/3re3beW8W

甚至:

int main(void)
{
    int rows;
    scanf("%d", &rows);
    for(int i=0;i<rows;i++,putchar('\n'))
        for(int j=0;j<=i;j++)
            printf(!j || i == rows - 1 || i == j ? "1 " : "0 ");
}

int main(void)
{
    int rows;
    scanf("%d", &rows);
    for(int i=0;i<rows;i++,putchar('\n'))
        for(int j=0;j<=i;j++)
            printf("%d ", !j || i == rows - 1 || i == j);
}
  for(j=0;j<=i;j++){
            if(i==j)
                printf("1 \n");
            else if(j==0 || i==row-1)
                printf("1 ");
            else
                printf("0 ");
  }

可以从您的代码中删除如此多的标记。