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

木蟲 (著名寫手)

木蟲 (著名寫手)

木蟲 (著名寫手)

木蟲 (著名寫手)

金蟲 (初入文壇)
|
樓主,你的程序有幾個(gè)小問題,給你稍微改了下,僅供參考,測(cè)試幾個(gè)例子,已通過。 #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ā)表 | |
|---|---|---|---|---|
|
[考研] 346求調(diào)劑 一志愿070303有機(jī)化學(xué) +5 | 蘿卜燉青菜 2026-03-28 | 6/300 |
|
|---|---|---|---|---|
|
[考研] 0703化學(xué) +14 | 妮妮ninicgb 2026-03-27 | 14/700 |
|
|
[考研] 327求調(diào)劑 +4 | 小卡不卡. 2026-03-29 | 4/200 |
|
|
[考研] 各位老師好,我的一志愿為北京科技大學(xué)085601材料專碩 +9 | Koxui 2026-03-28 | 9/450 |
|
|
[考研] 343求調(diào)劑085601 +3 | 要努力學(xué)習(xí)x 2026-03-29 | 3/150 |
|
|
[考研] 一志愿南昌大學(xué)324求調(diào)劑 +5 | hanamiko 2026-03-29 | 5/250 |
|
|
[考研] 356求調(diào)劑 +4 | gysy?s?a 2026-03-28 | 4/200 |
|
|
[考研] 279求調(diào)劑 +4 | 蝶舞輕繞 2026-03-29 | 4/200 |
|
|
[考研] 330分求調(diào)劑 +5 | qzenlc 2026-03-29 | 5/250 |
|
|
[考研] 347求調(diào)劑 +3 | 山頂見α 2026-03-25 | 3/150 |
|
|
[考研] 材料求調(diào)劑一志愿哈工大324 +7 | 閆旭東 2026-03-28 | 9/450 |
|
|
[考研] 086502化學(xué)工程342求調(diào)劑 +6 | 阿姨復(fù)古不過 2026-03-27 | 6/300 |
|
|
[考研] 086000調(diào)劑 +3 | 7901117076 2026-03-26 | 3/150 |
|
|
[考研] 266分求材料化工冶金礦業(yè)等專業(yè)的調(diào)劑 +4 | 哇呼哼呼哼 2026-03-26 | 4/200 |
|
|
[考研] 085600材料與化工調(diào)劑 +10 | A-哆啦Z夢(mèng) 2026-03-23 | 16/800 |
|
|
[考研] 0856調(diào)劑 +5 | 求求讓我有書讀?/a> 2026-03-26 | 6/300 |
|
|
[考研] 085600,材料與化工321分調(diào)劑 +4 | 大饞小子 2026-03-27 | 6/300 |
|
|
[考研] 305求調(diào)劑 +5 | 哇盧卡庫 2026-03-26 | 5/250 |
|
|
[考研] 考研調(diào)劑 +10 | 呼呼?~+123456 2026-03-24 | 10/500 |
|
|
[考研] 材料求調(diào)劑 +5 | .m.. 2026-03-25 | 5/250 |
|