| 9 | 1/1 | 返回列表 |
| 查看: 2030 | 回復: 8 | ||
renchong銀蟲 (小有名氣)
|
[求助]
一個區(qū)間搜索的c語言程序 出問題了
|
|
我寫了個搜索 大-小-大 區(qū)間的程序 ,但最后運行結(jié)果始終是錯的,不清楚哪里有問題(演算過 邏輯上是正確的)求蟲友們幫幫忙 #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; } |
木蟲 (知名作家)

木蟲 (著名寫手)

木蟲 (著名寫手)

木蟲 (著名寫手)

木蟲 (著名寫手)

金蟲 (初入文壇)
|
樓主,你的程序有幾個小問題,給你稍微改了下,僅供參考,測試幾個例子,已通過。 #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; } |
銀蟲 (小有名氣)
金蟲 (初入文壇)
| 9 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 0703化學321分求調(diào)劑 +9 | 三dd. 2026-03-30 | 10/500 |
|
|---|---|---|---|---|
|
[考研] 317分 一志愿南理工材料工程 本科湖工大 求調(diào)劑 +12 | 芋泥小鈴鐺 2026-03-28 | 12/600 |
|
|
[考研] 291求調(diào)劑 +8 | HanBeiNingZC 2026-03-24 | 8/400 |
|
|
[碩博家園] 求調(diào)劑 有機化學考研356分 +10 | Nadiums 2026-03-25 | 11/550 |
|
|
[考研] 282求調(diào)劑 +4 | wcq131415 2026-03-24 | 4/200 |
|
|
[考研] 279求調(diào)劑 +4 | 蝶舞輕繞 2026-03-29 | 4/200 |
|
|
[考研] 數(shù)一英一271專碩(085401)求調(diào)劑,可跨 +7 | 前行必有光 2026-03-28 | 8/400 |
|
|
[考研] 085602 化工專碩 338分 求調(diào)劑 +12 | 路癡小琪 2026-03-27 | 12/600 |
|
|
[考研] 0703化學求調(diào)劑,各位老師看看我!! +5 | 祁祺祺 2026-03-25 | 5/250 |
|
|
[考研] 279 分 求調(diào)劑 +4 | 睡個好覺_16 2026-03-24 | 4/200 |
|
|
[考研] 085600,材料與化工321分調(diào)劑 +4 | 大饞小子 2026-03-27 | 6/300 |
|
|
[考研] 348求調(diào)劑 +4 | 小懶蟲不懶了 2026-03-27 | 5/250 |
|
|
[考研] 298調(diào)劑 +3 | jiyingjie123 2026-03-27 | 3/150 |
|
|
[論文投稿] Journal of Mechanical Science and Technology +3 | Russ_ss 2026-03-25 | 5/250 |
|
|
[考研] 286求調(diào)劑 +4 | lim0922 2026-03-26 | 4/200 |
|
|
[考研] 081200-11408-276學碩求調(diào)劑 +3 | 崔wj 2026-03-26 | 3/150 |
|
|
[考研]
|
WWW西西弗斯 2026-03-24 | 8/400 |
|
|
[考研] 309求調(diào)劑 +4 | gajsj 2026-03-25 | 5/250 |
|
|
[考研] 0854人工智能方向招收調(diào)劑 +4 | 章小魚567 2026-03-24 | 4/200 |
|
|
[考研] 344求調(diào)劑 +3 | desto 2026-03-24 | 3/150 |
|