作者AlinChang (響往西岸Bay Life)
看板C_and_CPP
標題Re: [問題] 新手發問..第二彈
時間Sat Apr 25 18:01:41 2009
感謝上次幫我看code的先進們...
最近又碰到問提了...complair 又不過..我用DEV 4.9.9.2
題目是這樣的: 以迴圈方式計算,利用N條線,可將平面分成最多幾個區域? N >= 0;
我的解法如下:
#include <stdio.h>
#include <stdlib.h>
int cal (int key){
if (0==key){
return 1;
}else{
return cal(key+1)+key;
}
}
main(){
int x=0;
printf ("please insert a NO:");
scanf ("%d", x );
printf ("%d", cal(x));
system ("pause");
}
--
█ ▅ ╳╳╳╳╳╳╳ §釋憲 649╳╳╳ █
█ ▉ ▉ ╳◤ ╳ 賭神 ╳╳╳ █
█ ▄ ▂◥ ▋▂ ▇ ◥ ▉ ▎╳ 怖告欄 ╳ 連個 按摩╳╳╳ █
█ ▄ ◤  ̄▎ ▄ ▄ ◤▊▎╳ ╳ 都沒得幹了╳╳╳ █
▉ ◤ ▃▋ / ▆
▉
▆ ▉ ╳ 出租 ╳ ●●- ◢╳╳╳ █
▉ ▎ ◤ ▅▅▅ ▎ ▉ ╳ ◢╳╳by:Alin Chang╳ █
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.60.127.18
1F:推 netsphere:&x? 04/25 18:16
2F:→ netsphere:還有你的遞迴不會停 04/25 18:17
3F:→ AlinChang:sorry..哈哈.。我犯了一樣的錯 ...>< & 唉C..有點煩 04/25 18:21
4F:推 VictorTom:complair?? compile?? compile不過先看error/warning寫 04/25 18:37
5F:→ VictorTom:什麼, 看不懂英文就查字典, 看不懂中文就貼出來問.... 04/25 18:38
6F:→ VictorTom:main最好習慣 int main() 還有最後return 0什麼的.... 04/25 18:39
7F:→ VictorTom:話說, 題目要求用迴圈(loop), 可是你寫得是遞迴說 04/25 18:39
8F:→ VictorTom:(resursion)?? 題外話, 怎麼有的func是黃的有的是白的XD 04/25 18:41
9F:→ VictorTom: c 又拼錯Orz PS. 小弟我還不知道這題的數學解Q_Q~ 04/25 18:43
10F:→ AlinChang:return cal(key+1)+key; 改成 // (key-1)+Key 還有 &x 04/25 18:51
11F:→ AlinChang:這樣就順利"COMPILE"了..感謝樓上更正..數學作法的話.. 04/25 18:52
12F:→ netsphere:記的數學解好像是 F(x)=F(x-1)+(x-1)可以用數學規納法證 04/25 18:53
13F:→ AlinChang:我數學不好啦.。不過我的想法是: 0條線會有1個平面 04/25 18:53
14F:→ AlinChang:1條線會有 2 個區域;2條線最多會有 4個;3就有3+(3+1)個 04/25 18:53
15F:→ netsphere: x (?) 04/25 18:54
16F:推 snowlike:X1=2; Xn=Xn-1+N; ? 04/25 19:27
17F:→ snowlike:X0=1 修正orz 04/25 19:28
18F:→ VictorTom:我的重點不是你拼錯字, 而是compile有錯去看錯誤訊息. 04/25 22:27
19F:→ VictorTom:看不懂就貼錯誤訊息; 會這樣問是因為, &x與x+1雖然有邏 04/25 22:28
20F:→ VictorTom:輯上的錯誤, 但是你貼的code我照貼到DevC++ 4.9.9.2去 04/25 22:28
21F:→ VictorTom:build是可以過的, 只是執行時一開始就會因為程式企圖把 04/25 22:29
22F:→ VictorTom:使用者輸入寫到address 0x00去, 才產生了access 04/25 22:29
23F:→ VictorTom:violation然後程式掛掉. 所以你的問題應該是run-time 04/25 22:30
24F:→ VictorTom:error, 而不是compile time error. 04/25 22:31
25F:→ VictorTom:就算是run-time error也最好註明錯誤訊息, 雖然這一段 04/25 22:31
26F:→ VictorTom:code錯誤的地方還滿容易抓的. 04/25 22:31