| 9 | 1/1 | 返回列表 |
| 查看: 2028 | 回復(fù): 8 | ||
renchong銀蟲(chóng) (小有名氣)
|
[求助]
一個(gè)區(qū)間搜索的c語(yǔ)言程序 出問(wèn)題了
|
|
我寫了個(gè)搜索 大-小-大 區(qū)間的程序 ,但最后運(yùn)行結(jié)果始終是錯(cuò)的,不清楚哪里有問(wèn)題(演算過(guò) 邏輯上是正確的)求蟲(chóng)友們幫幫忙 #include main() { int a,b,x0,x1,x2,h,f0,f1,f2; printf("please input the number\n" ;scanf("x0=%d,h=%d",&x0,&h); x1=x0+h; f1=f(x1); f0=f(x0); if(f1 h=2*h; x2=x0+h; f2=f(x2); for(f1,f2;f2 x0=x1; x1=x2; x2=x1+h; f0=f(x0); f1=f(x1); f2=f(x2); } a=x0; b=x2; } else { h=-h; x2=x0+h; f2=f(x2); for(f0,f2;f2 x1=x0; x0=x2; x2=x0+h; f0=f(x0); f1=f(x1); f2=f(x2); } a=x2; b=x1; } printf("a=%d,b=%d\n",a,b); getch(); } int f(int n) { return 2+n^2; } |
木蟲(chóng) (知名作家)

木蟲(chóng) (著名寫手)

木蟲(chóng) (著名寫手)

木蟲(chóng) (著名寫手)

木蟲(chóng) (著名寫手)

金蟲(chóng) (初入文壇)
|
樓主,你的程序有幾個(gè)小問(wèn)題,給你稍微改了下,僅供參考,測(cè)試幾個(gè)例子,已通過(guò)。 #include int f(int n) { return 2 + n * n; } int main() { int a, b, x0, x1, x2, h, f0, f1, f2; printf("please input the number: x0 = " ;scanf("%d", &x0); printf("please input the number: h = " ;scanf("%d", &h); x1 = x0 + h; f1 = f(x1); f0 = f(x0); if(f1 < f0){ x2 = x1+h; f2 = f(x2); while(f2 < f1){ x0 = x1; x1 = x2; x2 = x1+h; f0 = f(x0); f1 = f(x1); f2 = f(x2); } a = x1; b = x2; } else{ x2 = x0 - h; f2 = f(x2); while(f2 < f0){ x1 = x0; x0 = x2; x2 = x0-h; f0=f(x0); f1=f(x1); f2=f(x2); } a=x2; b=x1; } printf("a = %d, b = %d\n", a, b); return 0; } |
銀蟲(chóng) (小有名氣)
金蟲(chóng) (初入文壇)
| 9 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 求調(diào)劑 +7 | 青春裁為三截 2026-03-29 | 7/350 |
|
|---|---|---|---|---|
|
[考研] 一志愿鄭大材料工程290求調(diào)劑 +4 | Youth_ 2026-03-30 | 4/200 |
|
|
[考研] 0703化學(xué)/290求調(diào)劑/本科經(jīng)歷豐富/工科也可 +13 | 丹青奶蓋 2026-03-26 | 15/750 |
|
|
[考研] 材料專碩調(diào)劑 +9 | 椰椰。 2026-03-29 | 9/450 |
|
|
[考研] 285求調(diào)劑 +5 | AZMK 2026-03-29 | 8/400 |
|
|
[考研] 一志愿河北工業(yè)大學(xué)0817化工278分求調(diào)劑 +14 | jhybd 2026-03-23 | 19/950 |
|
|
[考研]
|
nnnnnnn5 2026-03-25 | 11/550 |
|
|
[考研] 0703化學(xué)調(diào)劑,求導(dǎo)師收 +9 | 天天好運(yùn)來(lái)上岸?/a> 2026-03-24 | 10/500 |
|
|
[考研] 332求調(diào)劑 +4 | @MZB382400 2026-03-28 | 4/200 |
|
|
[考研] 生物學(xué)學(xué)碩,一志愿湖南大學(xué),初試成績(jī)338 +6 | YYYYYNNNNN 2026-03-26 | 7/350 |
|
|
[考研] 壓國(guó)家一區(qū)線,求導(dǎo)師收留,有恩必謝! +7 | 迷人的哈哈 2026-03-28 | 7/350 |
|
|
[考研] 一志愿南京航空航天大學(xué)材料學(xué)碩求調(diào)劑 +3 | @taotao 2026-03-28 | 3/150 |
|
|
[考研] 復(fù)試調(diào)劑,一志愿南農(nóng)083200食品科學(xué)與工程 +5 | XQTJZ 2026-03-26 | 5/250 |
|
|
[考研] 08開(kāi)頭275求調(diào)劑 +4 | 拉誰(shuí)不重要 2026-03-26 | 4/200 |
|
|
[考研] 085601 材料工程 313分 求調(diào)劑 +5 | Ong3 2026-03-27 | 5/250 |
|
|
[論文投稿] Journal of Mechanical Science and Technology +3 | Russ_ss 2026-03-25 | 5/250 |
|
|
[考研] 359求調(diào)劑 +4 | 王了個(gè)楠 2026-03-25 | 4/200 |
|
|
[考研] 343求調(diào)劑 +4 | 贈(zèng)我一本書(shū) 2026-03-23 | 4/200 |
|
|
[考研] 化學(xué)調(diào)劑一志愿上海交通大學(xué)336分-本科上海211 +4 | 小魚(yú)愛(ài)有機(jī) 2026-03-25 | 4/200 |
|
|
[考研] 080500求調(diào)劑 +3 | zzzzfan 2026-03-24 | 3/150 |
|