打印语句会多次打印而不是一次打印?

时间:2016-03-10 17:17:33

标签: c for-loop printf

这是我目前的代码:

2016/03/10 12:39:18 Failed to upload MultipartUpload: upload multipart failed
    upload id: QOWW4jBHH4PKjs1Tloc8dlCTtFN94vDHIJIWJChsrjxLZggScZbRUhM4FU9V.xOnIg9uYnBWqOA1x1xqStfA1p8vdAOHNyUp4gOO5b1gbuXvUitQyLdfFhKg9MnyxsV1
caused by: RequestError: send request failed
caused by: Put https://myBucket/myKey?partNumber=1&uploadId=QOWW4jBHH4PKjs1Tloc8dlCTtFN94vDHIJIWJChsrjxLZggScZbRUhM4FU9V.xOnIg9uYnBWqOA1x1xqStfA1p8vdAOHNyUp4gOO5b1gbuXvUitQyLdfFhKg9MnyxsV1: read |0: illegal seek
exit status 1

这是它的输入和输出:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char names[2048][32];
    int removed[2048];

    int game;
    int n;

    int m;
    int k;
    int l;

    int i;
    int j;
    int round;
    int pos;

    int lowest;
    int next;

    // Read the number of games to be played
    scanf("%d",&n);

    // Process each game
    for(game = 1; game <= n; game++)
    {

        // Read the number of friends
        scanf("%d",&m);

        // Read the names of each friend
        for(i=0;i<m;i++)
        {
            scanf("%s",names[i]);

            // Mark the friend as having not been eliminated
            removed[i] = 0;
        }

        // Read the number of rounds and the number of "Ducks"
        scanf("%d %d", &k,&l);

        // If the number of rounds is greater than the number of friends
        // Jimmy will boot all his friends
        if(k >= m)
        {
            printf("Jimmy has friends no more.\n\n");
            continue;
        }

        // Simulate the game round by round
        pos = 0;
        for(round=0;round<k;round++)
        {
            // Walk around the circle stopping at the lth remaining friend
            for(i=0; i<=l; pos = (pos+1)%m)
            {
                if(!removed[pos])
                    i++;

                if(i == l+1)
                    // Give the friend we stopped at the boot
                    removed[pos]=1;

            }
        }

        lowest = 0;
        // Loop for each remaining friend
        for(i=0;i<(m-k);i++)
        {
            // Find the lowest numbered remaining friend
            while(removed[lowest])
                lowest++;

            // Find the friend with the first name alphabetically
            next = lowest;
            for(j=lowest;j<m;j++)
            {
                if(!removed[j] && strcmp(names[j],names[next]) < 0)
                    next = j;
            }

            // Print the name of the next friend and remove him/her from the circle
            removed[next] = 1;
            printf("Game %d:\n", game);
            printf("%s\n", names[next]);
        }
        printf("\n");


    }
    return 0;
}

我怎样才能拥有&#34;游戏2:&#34; printf语句只显示一次?我已经尝试将它放在代码的不同部分,但我仍然没有得到它。为了更好地理解我想要获得的东西,这就是我想要做的事情:

Input:
2
3
Bob
Cody
John
2 2
Game 1: (the output)
Cody(the output)

8
Carol
Casey
Nick
Kirsten
Ben
Bo
Billy
Heather
3 4
Game 2: (The output)
Billy(The output)
Game 2:(The output)
Bo(The output)
Game 2:(The output)
Carol(The output)
Game 2:(The output)
Kirsten(The output)
Game 2:(The output)
Nick(The output)

1 个答案:

答案 0 :(得分:1)

移动&#34; Game&#34;在你的for循环之外:

    // print "Game" here
    printf("Game %d:\n", game);
    // Loop for each remaining friend
    for(i=0;i<(m-k);i++)
    {
        // Find the lowest numbered remaining friend
        while(removed[lowest])
            lowest++;

        // Find the friend with the first name alphabetically
        next = lowest;
        for(j=lowest;j<m;j++)
        {
            if(!removed[j] && strcmp(names[j],names[next]) < 0)
                next = j;
        }

        // Print the name of the next friend and remove him/her from the circle
        removed[next] = 1;
        // print "game" was here
        printf("%s\n", names[next]);
    }