又來了
請把下面這些程式碼複製到你電腦去執行
我以砍頭保證結果絕對正確
為什麼丟上去執行
結果會變成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;
}
又來了
請把下面這些程式碼複製到你電腦去執行
我以砍頭保證結果絕對正確
為什麼丟上去執行
結果會變成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軟體很好會自動避掉錯誤
又來了
請把下面這些程式碼複製到你電腦去執行
我以砍頭保證結果絕對正確
為什麼丟上去執行
結果會變成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/