如何缩短这个?有办法吗?

时间:2017-11-16 04:40:04

标签: c

我必须输出每个测试分数,每行5个,最后一个是三个。有没有办法做到这一点,如在更少的代码中?

int arrayDisplay(int testScore[], int size){

    printf("%d %d %d %d %d", testScore[0], testScore[1], testScore[2], testScore[3], testScore[4]);
    printf("\n");
    printf("%d %d %d %d %d", testScore[5], testScore[6], testScore[7], testScore[8], testScore[9]);
    printf("\n");
    printf("%d %d %d %d %d", testScore[10], testScore[11], testScore[12], testScore[13], testScore[14]);
    printf("\n");
    printf("%d %d %d %d %d", testScore[15], testScore[16], testScore[17], testScore[18], testScore[19]);
    printf("\n");
    printf("%d %d %d %d %d", testScore[20], testScore[21], testScore[22], testScore[23], testScore[23]);
    printf("\n");
    printf("%d %d %d %d %d", testScore[24], testScore[25], testScore[26], testScore[27], testScore[28]);
    printf("\n");
    printf("%d %d %d", testScore[29], testScore[30], testScore[31]);
    printf("\n");

    return 0;
}

1 个答案:

答案 0 :(得分:1)

我觉得这样做有点脏,但是:

    int arrayDisplay(int *testScore, int size)
    {
        for (int i = 0 ; i < size; i++)
        {
          printf("%d ", testScore[i]);
          if (i%5==0)
          {
            printf("\n");
          }

        }

    return 0;

    }

要明确:SO通常不会对这些类型的问题表示友好。考虑一下这是你的一个;)