可以幫我解答空格中的答安案是什麼嗎^^

1. 模擬產生6個介於3-49的樂透彩號碼並印出排序前後情形

請問第 3 空白處為何?

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define TOTALCARD __(1)__

#define HAND 6

int main()

{

int I,j, temp,point, card[HAND], sw[TOTALCARD];

long time_t;

srand(time(&time_t));

for (i=0; i<TOTALCARD; i++)

sw[i] = 0;

for (i=0; i<HAND; i++)

{

while (1)

{

point = rand() % TOTALCARD + __(2)__;

if (sw[point]==0) __(3)__;

}

card[i] = point;

sw[point] = 1;

}

printf("排序前: ");

for (i=0; i<HAND; i++)

printf("%3d", card[i]);

printf("\n");

for (i=0;I < HAND-1;i++)

for(j=i+1;j< HAND;j++)

if (card[i] __(4)__ card[j])

{

temp = card[i];

card[i] = card[j];

card[j] = __(5)__;

};

printf("排序後: ");

for (i=0; i<HAND; i++)

printf("%3d", card[i]);

printf("\n");

return 0;

}

2 個解答

評分
  • 慧雅
    Lv 5
    9 年前
    最佳解答

    #include <stdio.h>

    #include <stdlib.h>

    #include <time.h>

    #define TOTALCARD 47

    #define HAND 6

    int main()

    {

    int i,j, temp,point, card[HAND], sw[TOTALCARD];

    long time_t;

    srand(time(&time_t));

    for (i=0; i<TOTALCARD; i++)

    sw[i] = 0;

    for (i=0; i<HAND; i++)

    {

    while (1)

    {

    point = rand() % TOTALCARD + 3;

    if (sw[point]==0) break;

    }

    card[i] = point;

    sw[point] = 1;

    }

    printf("排序前: ");

    for (i=0; i<HAND; i++)

    printf("%3d", card[i]);

    printf("\n");

    for (i=0;i < HAND-1;i++)

    for(j=i+1;j< HAND;j++)

    if (card[i] <card[j])

    {

    temp = card[i];

    card[i] = card[j];

    card[j] = temp;

    };

    printf("排序後: ");

    for (i=0; i<HAND; i++)

    printf("%3d", card[i]);

    printf("\n");

    system("pause");

    //return 0;

    }

  • 9 年前

    寫在前面:

    1. 這個題目用填空題沒辦法得到一個符合要求卻沒有錯誤的答案。

    2. 樂透號碼是從1開始,為什麼要從3開始,是題目寫錯?還是有什麼用意?

    3. 你的原始程式碼裡面,變數i 的大小寫不分,以下已修正。程式也重新對齊。

    最接近的答案是以下這個,排序由小到大。

    第三個空格要填的是 break

    ==============

    #include

    #include

    #include

    #define TOTALCARD 47

    #define HAND 6

    int main()

    {

    int i,j, temp,point, card[HAND], sw[TOTALCARD];

    long time_t;

    srand(time(&time_t));

    for (i=0; i

    sw[i] = 0;

    for (i=0; i

    {

    while (1)

    {

    point = rand() % TOTALCARD + 3;

    if (sw[point]==0) break;

    }

    card[i] = point;

    sw[point] = 1;

    }

    printf("排序前: ");

    for (i=0; i

    printf("%3d", card[i]);

    printf("\n");

    for (i=0;i < HAND-1;i++)

    for(j=i+1;j< HAND;j++)

    if (card[i] > card[j])

    {

    temp = card[i];

    card[i] = card[j];

    card[j] = temp;

    };

    printf("排序後: ");

    for (i=0; i

    printf("%3d", card[i]);

    printf("\n");

    printf("=============================\n");

    return 0;

    }

    ==============

    這種寫法的問題在於以下四行互相抵觸:

    #define TOTALCARD 47

    int sw[TOTALCARD];

    point = rand() % TOTALCARD + 3;

    sw[point] = 1; // 當point = 47, 48, 49 時會超出原先的陣列 (合法範圍為0~46)

    所以在執行上可能會出問題。之所以說『可能』是因為超出陣列範圍所引發的反應是不可預期的,有可能永遠跑不出 47, 48, 49,也有可能會跑出47, 48, 49,但是多跑幾次之後就會出現錯誤。

    如果不受限於空格的話,以下是一個沒有語法錯誤的版本,可以正確地產生3~49的號碼:

    (修改地方以紅色標示)

    #include

    #include

    #include

    #define TOTALCARD 47

    #define HAND 6

    int main()

    {

    int i,j, temp,point, card[HAND], sw[TOTALCARD];

    long time_t;

    srand(time(&time_t));

    for (i=0; i

    sw[i] = 0;

    for (i=0; i

    {

    while (1)

    {

    point = rand() % TOTALCARD;

    if (sw[point]==0) break;

    }

    sw[point] = 1;

    card[i] = point+3;

    }

    printf("排序前: ");

    for (i=0; i

    printf("%3d", card[i]);

    printf("\n");

    for (i=0;i < HAND-1;i++)

    for(j=i+1;j< HAND;j++)

    if (card[i] > card[j])

    {

    temp = card[i];

    card[i] = card[j];

    card[j] = temp;

    };

    printf("排序後: ");

    for (i=0; i

    printf("%3d", card[i]);

    printf("\n");

    printf("=============================\n");

    return 0;

    }

還有問題?馬上發問,尋求解答。