#7837: 我以砍頭保證結果絕對正確


wemee (阿勇)

學校 : 國立北港高級中學
編號 : 4621
來源 : [220.135.3.103]
最後登入時間 :
2023-09-27 11:01:07
a251. 假費波那契數 -- 2011成功高中校內賽初賽第一題 | From: [60.250.139.121] | 發表日期 : 2013-06-07 17:35

又來了

請把下面這些程式碼複製到你電腦去執行

我以砍頭保證結果絕對正確

為什麼丟上去執行

結果會變成WA 一堆怪異的數字

#include<stdio.h>

 

void bubbleSort(int arr[], int count){

    int i = count, j;

    int temp;

    

    while(i > 0){

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

        {

            if(arr[j] > arr[j + 1])

            {   temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

        i--;

    }

}

 

int main() {

    int t, i, j, s[4], n, *fab;

    while( scanf("%d", &t)!=EOF ) {

        for(i=0; i<t; i++){

            scanf("%d %d %d %d %d", &n, &s[0], &s[1], &s[2], &s[3]);

            fab = malloc(n*sizeof(int));

            memcpy(fab, s, n*sizeof(int)/sizeof(char));

            for(j=4; j<n; j++) {

                fab[j] = fab[j-1] + fab[j-4];

            }

            bubbleSort(fab, n);

            printf("%d", fab[n/2]);

            free(fab);

        }

    }

    return 0;

} 

 
#7839: Re:我以砍頭保證結果絕對正確


akira0331 (小迷糊)

學校 : 不指定學校
編號 : 26613
來源 : [203.70.194.240]
最後登入時間 :
2013-07-29 09:30:29
a251. 假費波那契數 -- 2011成功高中校內賽初賽第一題 | From: [203.70.194.240] | 發表日期 : 2013-06-07 17:51

又來了

請把下面這些程式碼複製到你電腦去執行

我以砍頭保證結果絕對正確

為什麼丟上去執行

結果會變成WA 一堆怪異的數字

#include

 

void bubbleSort(int arr[], int count){

    int i = count, j;

    int temp;

 

    while(i > 0){

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

        {

            if(arr[j] > arr[j + 1])

            {   temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

        i--;

    }

}

 

int main() {

    int t, i, j, s[4], n, *fab;

    while( scanf("%d", &t)!=EOF ) {

        for(i=0; i

            scanf("%d %d %d %d %d", &n, &s[0], &s[1], &s[2], &s[3]);

            fab = malloc(n*sizeof(int));

            memcpy(fab, s, n*sizeof(int)/sizeof(char));

            for(j=4; j

                fab[j] = fab[j-1] + fab[j-4];

            }

            bubbleSort(fab, n);

            printf("%d", fab[n/2]);

            free(fab);

        }

    }

    return 0;

} 


我印象中malloc函數是放在stdlib.h,而memcpy函數是放在string.h

你的程式不include 這兩個標頭檔,只能說你電腦的combile軟體很好會自動避掉錯誤

 
#7844: Re:我以砍頭保證結果絕對正確


example (學姊)

學校 : 臺北市立麗山高級中學
編號 : 6634
來源 : [60.250.138.144]
最後登入時間 :
2022-08-09 17:07:42
a251. 假費波那契數 -- 2011成功高中校內賽初賽第一題 | From: [220.137.11.29] | 發表日期 : 2013-06-09 00:35

又來了

請把下面這些程式碼複製到你電腦去執行

我以砍頭保證結果絕對正確

為什麼丟上去執行

結果會變成WA 一堆怪異的數字

標題十分聳動XD

用 gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 編譯的結果為

test.c: In function ‘main’:

test.c:25:13: warning: incompatible implicit declaration of built-in function ‘malloc’

test.c:26:7: warning: incompatible implicit declaration of built-in function ‘memcpy’

test.c:32:7: warning: incompatible implicit declaration of built-in function ‘free’

用 g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 編譯的結果為

test.c: In function ‘int main()’:

test.c:25:33: error: ‘malloc’ was not declared in this scope

test.c:26:48: error: ‘memcpy’ was not declared in this scope

test.c:32:15: error: ‘free’ was not declared in this scope 

跟樓上大大講的原因一樣

你可以去參考這兩個網頁

http://www.cplusplus.com/reference/cstdio/

http://www.cplusplus.com/reference/cstdlib/ 

 
ZeroJudge Forum