實驗4迴圈控制

2023-01-02 02:12:04 字數 1620 閱讀 1933

一:目的要求

1.熟悉用while語句,do-while 語句和for語句實現迴圈的方法;

2.掌握在程式設計中用迴圈方法實現各種演算法;

3.掌握計算程式執行所佔機時的計算方法;

4.上機前按實驗要求預習編寫出完整的程式,才允許上機。

二:實驗內容與步驟

100匹馬馱100擔貨,大馬一匹馱3擔,中馬一匹馱2擔,小馬兩匹馱一擔。

1.當大馬、中馬、小馬每種不能少於一匹,有多少種組合法?

2.當可以缺少一種或缺二種,又有多少種組合法?

3.用while求第一種組合法。

4.用do-while求2組合法。

5.用三重或二重for迴圈求2種組合法。

6.除列印結果和多少種組合法外,還要分別列印三種演算法所費機時多少。

三:計算一種演算法所佔機時的程式提示:

#include <>

#include <>

#include <>

main()

end=clockend = time();*/

printf("the num of method1 is: %d\n",ncount);

printf("and the time is: %5.1f time\n",difftime(end,start));

/*printf f(「」the difference is :%5.1f second\n」, difftime(end,start)/18.2);*/.}

1、while方法

#include <>

#include <>

#include <>

#include <>

void main()

}big++;

} end=clock();

printf("the num of method1 is: %d\n",ncount);

printf("and the time is: %5.1f time\n",difftime(end,start));

getchar();

}2、do-while方法

#include <>

#include <>

#include <>

#include <>

void main()

}big++;

}while(big<=34);

end=clock();

printf("the num of method 2 is: %d\n",ncount);

printf("and the time is: %5.1f time\n",difftime(end,start));

getchar();

}3、for迴圈法

#include <>

#include <>

#include <>

#include <>

void main()}}

end=clock();

printf("the num of method 3 is: %d\n",ncount);

printf("and the time is: %5.1f time\n",difftime(end,start));

getchar();}

VB實驗4迴圈結構

實驗內容 1.累加 設s 1 2 3 n,程式設計計算s的值,n的值程式執行時輸入,要求用for next實現。窗體檔案命名為專案檔案命名為 將及上傳至該個人網路儲存空間。2.階乘 程式設計用do loop迴圈計算除乘 n n的值程式執行時輸入,參考介面如下 窗體檔案命名為專案檔案命名為 將及上傳至...

實驗4 迴圈結構程式設計答案

班級學號姓名 完成日期課 內 外總計本實驗用時間 一 實驗目的 1 熟練掌握while語句,do while語句和for語句的格式和功能,能夠用這些語句實現迴圈結構程式設計的方法。2 熟練掌握迴圈語句巢狀使用的基本形式,能夠用迴圈語句實現多重迴圈。3 掌握在迴圈結構程式設計中使用break語句和co...

4實驗四迴圈結構程式設計

一 實驗目的 1 學習迴圈語句for while和do while語句的使用方法。2 學習用迴圈語句實現各種演算法,例如窮舉法 迭代法等。3 進一步熟悉tc整合環境的使用方法。二 實驗內容 1 下列程式計算sum的值。除錯該程式,使之能正確地計算sum。寫出除錯過程。計算公式如下 main int ...