[問題] 求integer長度
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Windows
問題(Question):
求變數a的長度
餵入的資料(Input):
a = 9
預期的正確結果(Expected Output):
1
錯誤結果(Wrong Output):
309
程式碼(Code):(請善用置底文網頁, 記得排版)
int main()
{
int a = 9;
int count = 1;
while(a / pow(10.0, count) != 0) {
count++;
}
cout << count << endl;
system("PAUSE");
return 0;
}
補充說明(Supplement):
有把cout << count; 放入while中,結果是從0加到309
是我的想法有錯嗎?
想法:
變數a,9 / 10的一次方,結果為0那他連while都不會進入
直接cout 印出1
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.192.24.211
→
12/14 19:34, , 1F
12/14 19:34, 1F
改成static_cast<int>a / pow(10.0, count)就可以了
學到了 int / double = double
※ 編輯: nature23306 來自: 123.192.24.211 (12/14 19:42)
推
12/14 23:26, , 2F
12/14 23:26, 2F
→
12/15 03:12, , 3F
12/15 03:12, 3F
→
12/16 18:45, , 4F
12/16 18:45, 4F
→
12/16 18:48, , 5F
12/16 18:48, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章