氣泡排序140.126.122.189/upload/1081/b10201a20196121745331.pdf · 2.將一組5...

15
氣泡排序 #include <stdio.h> int main() { int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 }; int len = (int) sizeof(arr) / sizeof(*arr); int i, j, temp; for (i = 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++) if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } for (i = 0; i < len; i++) printf("%d ", arr[i]); return 0; }

Upload: others

Post on 21-Oct-2019

7 views

Category:

Documents


0 download

TRANSCRIPT

氣泡排序

#include <stdio.h>

int main()

{

int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 };

int len = (int) sizeof(arr) / sizeof(*arr);

int i, j, temp;

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

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

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

temp = arr[j];

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

arr[j + 1] = temp;

}

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

printf("%d ", arr[i]);

return 0;

}

河內塔

#include<stdio.h>

#include<stdlib.h>

void towers(int n,char source,char aux,char dest)

{

static int step=0;//靜態 int 變數

printf("Towers( %d , %c , %c , %c ) \n",n,source, aux, dest);

if (n==1)//特例

printf("\t\t\t\t\tStep %d : Move from %c to %c \n",++step,source,dest);

else//一般式

{

towers(n-1, source, dest, aux);

printf("\t\t\t\t\tStep %d : Move from %c to %c \n",++step,source,dest);

towers(n-1, aux, source, dest);

}

}

int main(void)

{ int numDisks;

printf("Please enter number of disks:");

scanf("%d",&numDisks);

printf("Start Towers of Hanoi\n\n");

towers (numDisks, 'C', 'B', 'A');

// 碟子數量 起點 終點(目的) 輔助(temp)

system("pause");

return 0;

}

二維陣列指標

#include<stdio.h>

#include<stdlib.h>

#include<iostream>

using namespace std;

int main()

{

int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};

int i=0,j=0;

printf("a=%d\n",a);

printf("&a=%d\n",&a);

printf("*a=%d\n",*a);

printf("a[0]=%d\n",a[0]);

printf("&a[0]=%d\n",&a[0]);

printf("*a[0]=%d\n",*a[0]);

printf("a[0]=%d\n",&a[0]);

printf("a[1]=%d\n",&a[1]);

printf("a[2]=%d\n",&a[2]);

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

{

for(j=0;j<=2;j++)

{

printf("%d",*(*(a+i)+j));

}

printf("\n");

}

system("pause");

return 0;

}

記憶體空間範例

#include <iostream>

#include <stdlib.h>

using namespace std;

int main(void)

{ int *ptri; double *ptrd;

printf("sizeof(ptri)=%d\n",sizeof(ptri));

printf("sizeof(ptrd)=%d\n",sizeof(ptrd));

printf("sizeof(*ptri)=%d\n",sizeof(*ptri));

printf("sizeof(*ptrd)=%d\n",sizeof(*ptrd));

system("pause"); return 0;

}

函式傳陣列

#include<stdio.h>

#include<stdlib.h>

#define SIZE 4

void show(int arr[]);

int main(void)

{

int A[SIZE]={5,3,6,1};

printf("陣列的內容為: ");

show(A);

system("pause");

return 0;

}

void show(int arr[])

{

int i;

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

printf("%d ",arr[i]);

printf("\n");

}

指標範例

#include<stdio.h>

#include<iostream>

int main()

{

int a=0;//空間大小

printf("a 的地址是%d\n",&a);//取地址

int *ptr;//指標 pointer

ptr=&a;//a 的地址餵給 ptr

printf("*ptr=%d\n",*ptr);

printf("a=%d\n",a);

printf("ptr=%d\n",ptr);

printf("&a=%d\n",&a);

system("pause");

return 0;

}

遞迴範例

#include<iostream>

#include<stdio.h>

using namespace std;

void dec2bin(int a)

{ if(a==1)

printf("%d",a);

else

{ dec2bin(a/2);

printf("%d",a%2);;

}

}

int main(void)

{

int a;

printf("input an integer a:\n");

scanf("%d",&a);

dec2bin(a);

system("pause");

return 0;

}

OX 遊戲範例

#include<stdio.h>

#include<iostream>

using namespace std;

char output(int);

int array[9]={1,2,3,4,5,6,7,8,9};

main()

{

int i=0,k=0,x=0;

for(k=1;k<=9;k++)

{

printf("歡迎測試 tic-tac-toe 小遊戲測試第一版\n");

printf("第%d",k);

printf("請輸入 x 座標:\n");

scanf("%d",&x);

if(k%2==1) //1%2=1 2%2=0 3%2=1

array[x-1]=50;//錯誤

else//1%2=1 2%2=0 3%2=1

array[x-1]=100;//錯誤

printf("---------\n"); //c char 'X'

printf("|%c|%c|%c|\n",output(0),output(1),output(2));

printf("---------\n");

printf("|%c|%c|%c|\n",output(3),output(4),output(5));

printf("---------\n");

printf("|%c|%c|%c|\n",output(6),output(7),output(8));

printf("---------\n");

}

system("pause");

return 0;

}

char output(int i)//function 函數 函式 副程式

//回傳型態 char 函數名稱 output (傳入型態 int 變數 i, 傳入型態 2 變數 2)

{

if(array[i]==50) return 'X';

if(array[i]==100) return 'O';

else

return ' ';

}

/* prog9_5, 比較陣列元素值的大小 */

#include <stdio.h&rt;

#include <stdlib.h&rt;

int main(void)

{

int A[5]={74,48,30,17,62};

int i,min,max;

min=max=A[0]; /* 將 max 與 min 均設為陣列的第一個元素 */

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

{

if(A[i]&rt;max) /* 判斷 A[i]是否大於 max */

max=A[i];

if(A[i]<min) /* 判斷 A[i]是否小於 min */

min=A[i];

}

printf("陣列裡元素的最大值為%d\n",max);

printf("陣列裡元素的最小值為%d\n",min);

system("pause");

return 0;

}

取亂數範例

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main(void)

{

int a;

srand(time(NULL));

a=(rand()%9999)+1;

printf("The Random Number is %d .\n", a);

system("pause");

return 0;

}

測驗題 作答區

1. 請用另一種表達方式表達相同的下列運算式 (3%)

a. temp /=2 b. chu %= 100 c. amount *= cnt1 + cnt2

2. 請用另三種不同方式寫出整數變數 x 減 1 的 C 程式敘述,如下:

x- - (3%)

3. 請用 C 程式語言敘述完成下列工作 (2%)

a. 宣告兩個整數變數 sum 及 x

b. 設定變數 x 的值為 1

c. 設定變數 sum 的值為 0

d. 將變數 x 的值加至變數 sum 中,並將結果指定給變數 sum

e. 列印出 ‘’’The sum is : ’及變數 sum 的值

4. 請寫出下列程式之輸出結果 (2%)

char symbol[3] = {'a', 'c', 'b'};

for (int index = 0; index < 3 ; index++)

printf(" %c",symbol[index]);

5. 請寫出下列程式碼的輸出結果 (2%)

char a,b,c; a=‘c’; b=’a’; c=’b’;

printf("%c %c %c c", a, b, c);

6. 請寫出下列程式碼的輸出結果(4%)

int n=2 ; int valueProduced = 3*(++n) - - ; printf( "%d\n",valueProduced) ; printf( "%d\n",n) ;

7. 請完成下列程式輸出結果

(1) 下列程式需輸出由 11 至 1 的整數值 (需改寫程式) (3%)

for (int x=9;x>=1;x - =2) printf( "%d\n",x) ;

(2) 輸出 2 至 31 的奇數整數值 (需改寫程式) (3%)

int counter = 2; do { printf( "%d\n",counter) ; counter += 2; } while (counter <21);

(3) 請寫出輸出結果 (3%)

int c=1; int product=3; while (c<=5) { product *=c; c++; }

104-2 程式設計(一) 紙筆練習

printf( "%d",product) ;

8. 寫出下列程式輸出結果

(1) int x=1 ; (3%) while (x<=10) { x++ ;} printf( "%d",x) ;

(2) for (double y=.1 ;y !=0.6 ;y+=.1) (3%) printf( "%d\n",y) ;

(3) int n; (3%) (請先輸入一個任意數值,寫出輸出)

scanf("%d",&n); switch (1)

{ case 1 : printf( " The number is 1\n") ; break; case 2 : printf( " The number is 2\n") ; break; case 3 : printf( " The number is 1 or 2\n") ; break; }

9. (1)請寫出下列變數值或位址。(10%)(假設起始位址 0x22ff84)

a=____________ &a=___________

b=____________ &b=___________

ptr1=_________ &ptr1=__________ *ptr1=________

ptr2=_________ &ptr2=__________ *ptr2=________

(2)請畫出程式執行到最後的記憶體關係圖。(5%)

int main(void)

{ int a=10,b=8, *ptr1,*ptr2;

ptr1=&a;

ptr2=&b;

*ptr1=17;

*ptr2=25;

ptr2=ptr1;

a=7;

*ptr1=2;

a=6;

*ptr1=*ptr2+*ptr1;

*ptr2=*ptr2+*ptr1;

system("pause"); return 0;

}

班級:__________ 姓名:_____________

10. 請寫出左列程式的輸出結果,並且說明原因。(8%)

int main(void)

{ int *ptri; double *ptrd;

printf("sizeof(ptri)=%d\n", sizeof(ptri));

printf("sizeof(ptrd)=%d\n", sizeof(ptrd));

printf("sizeof(*ptri)=%d\n", sizeof(*ptri));

printf("sizeof(*ptrd)=%d\n", sizeof(*ptrd));

system("pause"); return 0; }

11. 請寫出以下的輸出結果(8%)

int main(void)

{ int x;

int a[10]={12,11,10,9,8,7,6,5,4,3};

int *p;

p=&x;

x=75;

*p=10;

p=a+8;

printf( " x=%d\n",x) ;

printf( "*p=%d\n",*p) ;

system("pause"); return 0; }

12.以下遞迴程式,請畫出 n=4 遞迴的過程,以一個方框代表呼叫一次函式。

(15%)

int fact(int a)

{ if(a>0) return (a*fact(a-1));

else return 1; }

int main(void)

{ int a;

do{

printf("input an integer:\n");

scanf("%d",&a);

}while(a<=0)

printf(”1*2*…*%d=%d",a,fact(a) }

13.此圖為一個二維陣列,請用迴圈語法寫程式印出 3, 5,7。(10%)

1 2 3

4 5 6

7 8 9

上機練習 *請將以前的作業題目進行練習

1.停車場狀態顯示

(1)設計 10 個車位,可輸入停車號碼表示停車,

(2)可重複輸入停車編號,辨識該號碼是否已停車

(3)顯示目前剩下車位數,顯示停車場顯示狀態

2.將一組 5 個整數陣列數字,依序從小到大排序好,原序列是沒排序的數列。

3. 請試寫一個遞迴函數 int sum(int n),會先讓使用者在主程式中輸入一個介於 3~9 的數字

n,如果超過此範圍會產生錯誤訊息,若符合 3~9 的數字,則呼叫 sum(n),然後 sum()函數會

利用遞迴函數(函數自己呼叫自己的方式),計算出 1*2 + 2 * 3 + 3 * 4 + 4 * 5 + ....( n - 1) * n

的結果,並將結果回傳至 main()主程式中顯示。

4. 把主程式內的陣列 a[6]={0,1,2,3,4,5},利用指標方式,在函式內改成 a[6]={0,0,0,0,0,0}。

5. 讓使用者可以輸入 5 筆學生考試的資料,每一位學生的輸入資料包括學號、姓名、程設分

數、會計分數、計概分數 ,輸入完一筆資料後,可以繼續輸入下一筆,直到使用者在輸入

學號。

每一筆輸入資料的格式範例如下,而此程式需要將使用者輸入的分數資料儲存至以 score 命

名的二維陣列變數中。

[畫面範例]

請依照[學號、姓名、程設分數、會計分數、計概分數] 順序輸入學生成績::

B09920388 應小熊 100 95 88

B09920366 王小華 86 87 76

6. 輸入 5 筆資料, 求出平均成績與加總成績

7.建立一個二維陣列, 5*5 的陣列, 請將 1-25 依序填入, 並印出 5*5 的陣列

8. 用遞迴方式解, 輸入 n, 輸出第 n 個數字

f(1)=1 f(2)=1 f(3)=2 f(n)=f(n-1)+f(n-2)+f(n-3)

9. n! n 階乘 輸入 n 輸出 n!

10. a 的 b 次方, 輸入 a,b 輸出 a 的 b 次方

11.請使用 for 指令設計一程式讓使用者輸入 n,並呼叫一自訂函數,印出下面的圖案(下圖為

n=5 時顯示的 n 層圖形)

543212345

4321 1234

321 123

21 12

1 1

12. 四則運算用函式,請用字元辨識

13. 試撰寫一支程式 int prime(int n)可找出第 n 個質數,質數是指 1 與自己可整除,請找出第

100 個質數

14. 請你使用 do...while 迴圈指令幫她寫一程式能找出 100~999 中,有幾組數字,其三個數字

的平方之和正好等於該數減去 100,即 XYZ - 100 = X 平方 + Y 平方 + Z 平方

15. 主程式 main 呼叫 func1 接收 func1 回傳 印出 BMI

func1 輸入身高, 體重 計算 BMI 回傳 BMI 回主程式

16. 主程式 main

輸入 a,b,c

呼叫 func1

呼叫 func2

接收 func1 回傳

印出 a+b

接收 func2 回傳

印出 a*b*c

func1 計算 a+b 回傳 a+b 回主程式

func2 計算 a*b*c 回傳 a*b*c 回主程式