/*請在程序的下劃線處填入正確的內容并把下劃線刪除,使程序得出正確的結果。注意:不得增行或刪行,也不得更改程序的結構!函數int day(int k,int m,int n),其功能是:返回小蠶需要多少天才能爬到樹頂(樹高k厘米,小蠶每天白天向上爬m厘米,每天晚上因睡覺向下滑n厘米,爬到頂后不再下滑)(n<m<k)。 例如,若分別 輸入253、71、29給 k、m、n,則輸出結果為:6。 */#include <conio.h>#include <stdio.h>#include <windows.h>int day( int k, int m, int n ){ int days, height = 0; for(__1___;; days ++) /*for循環用來統計天數*/ { height += m; /*height用于計算小蠶向上爬的高度*/ if ( __2___) return days; /*如果小蠶白天爬到了樹頂,則返加天數days*/ height -= n; /*否則到晚上小蠶因睡覺向下滑n厘米*/ }}main(){ int k, m, n; system("cls"); printf("\nPlease enter 3 numbers: "); scanf("%d %d %d", &k, &m, &n ); printf( "\nFor %d days, worm will be at the top.",day(k,m,n));}
2 回答

慕斯王
TA貢獻1864條經驗 獲得超2個贊
1. days = 1
2. height >= k
其實這很簡單的算法問題,int t =(k-m)/(m-n) ,如果能整除則days=t+1,不能則days=t+2。
調試如下: 與題意無關的語句我暫時屏蔽掉,你要用的時候再開啟便是。
/*#include <conio.h> */
/*#include <stdio.h> */
/*#include <windows.h> */
int day( int k, int m, int n )
{
int days, height = 0;
for(days=1;; days ++) /*for循環用來統計天數*/
{
height += m; /*height用于計算小蠶向上爬的高度*/
if (height>=k) return days; /*如果小蠶白天爬到了樹頂,則返加天數days*/
height -= n; /*否則到晚上小蠶因睡覺向下滑n厘米*/
}
}
main()
{
int k, m, n;
/* system("cls"); */
printf("\nPlease enter 3 numbers: ");
scanf("%d %d %d", &k, &m, &n );
printf( "\nFor %d days, worm will be at the top.",day(k,m,n));
getch();
}
添加回答
舉報
0/150
提交
取消