線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3765
推到 Plurk!
推到 Facebook!

有沒有一個函數可以無條件進位 1/3 => 1, 2/3 => 1

答題得分者是:StrongLemon
blk5743
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-24 09:37:58 IP:61.66.xxx.xxx 未訂閱
請問一下,我有一個相除的式子,結果可能不會整除 有沒有一個函數可以滿足下列需求 1/3=1, 2/3=1, 3/3=1, 4/3=2, 5/3=2, 6/3=2,.....
stellos
中階會員


發表:24
回覆:84
積分:51
註冊:2004-06-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-24 10:16:47 IP:61.60.xxx.xxx 未訂閱
int i=1,j=3; int a,b; a=1/3; b=1%3; if(b > 0) a ; 這樣就無條件進入啦
stellos
中階會員


發表:24
回覆:84
積分:51
註冊:2004-06-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-24 10:17:55 IP:61.60.xxx.xxx 未訂閱
抱歉打錯 int i=1,j=3; int a,b; a=i/j; b=i%j; if(b > 0) a ; 這樣就無條件進入啦
blk5743
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-24 10:36:34 IP:61.66.xxx.xxx 未訂閱
我現在也是用這個方式 不過我想應該有一個函式可以做這件事吧 下面是我用的方法 int a,b; a = 1/3; if ( 1%3 != 0 ) a ;
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-24 11:26:32 IP:220.130.xxx.xxx 未訂閱
for(int i=1; i<100; i  )
  int k := (i-1) / 3   1;
  
StrongLemon
高階會員


發表:10
回覆:166
積分:105
註冊:2004-04-18

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-12-24 11:56:34 IP:221.169.xxx.xxx 未訂閱
您好,以下節錄自Delphi Help..跟C Builder應該是一樣的 Truncates a real number to an integer. Delphi syntax: function Trunc(X: Extended): Int64; Rounds variables toward negative infinity. Delphi syntax: function Floor(const X: Extended): Integer; Description Call Floor to obtain the highest integer less than or equal to X. For example: Floor(-2.8) = -3 Floor(2.8) = 2 Floor(-1.0) = -1 Rounds variables up toward positive infinity. function Ceil(const X: Extended):Integer; Description Call Ceil to obtain the lowest integer greater than or equal to X. The absolute value of X must be less than MaxInt. For example: Ceil(-2.8) = -2 Ceil(2.8) = 3 Ceil(-1.0) = -1 Returns the value of X rounded to the nearest whole number. Delphi syntax: function Round(X: Extended): Int64; Rounds a floating-point value to a specified digit or power of ten using ?anker? rounding? function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double; Expression Value RoundTo(1234567, 3) 1234000 RoundTo(1.234, -2) 1.23 RoundTo(1.235, -2) 1.24 RoundTo(1.245, -2) 1.24
blk5743
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-12-24 13:34:21 IP:61.66.xxx.xxx 未訂閱
引言: for(int i=1; i<100; i ) int k = (i-1) / 3 1;
我覺得這方法不錯,但就怕(i-1) < 0會有問題 例(0-1)/3 1 => 1 StrongLemon的方式我試過了,可以滿足需求 ceil((double)0/(double)3)=0, ceil((double)1/(double)3)=1, ceil((double)2/(double)3)=1,
系統時間:2024-06-28 19:39:50
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!