[英]Error in extracting digits from integer
我有一個簡單的程序,它給出了錯誤的輸出,預期的輸出是數字的數字。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int n = 125521;
int d = floor(log10(n));
printf("%d Digits\n",d+1);
int t =0;
while(floor(log10(n))-t)
{ printf("%d-----%d\n",(n/(int)pow(10,floor(log10(n))-t)%10),t); t++;}
return 0;
}
這給出了輸出
6 Digits
1-----0
2-----1
5-----2
7-----3
2-----4
奇怪的輸出。 為什么7來了?
我知道如何通過其他方式獲取數字,但我希望這個解決方案起作用。
現在,正如答案中所建議的,我擺脫了 while 循環中的錯誤 (>=0),並得到了輸出:
在此行中放置適當的空格(n/(int)pow(10,floor(log10(n))-t)%10)
。
這么多多余的floor(log10(n))
調用。
while loop
將從數字中循環小於1
。
我不知道,為什么除了數字之外還要打印t
,請說:
1-----0
2-----1
^^^
無法重現你所說的。
你在找這個嗎:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
int main() {
int n = 550052111;
int d = floor(log10(n));
printf("%d Digits\n", d + 1);
int t = 0;
int power, divition, mod;
int digitCount[10] = {0};
while (d +1 - t) {
power = (int) pow(10, d - t);
divition = n / power;
mod = divition % 10;
digitCount[mod]++;
t++;
}
for (t= 0; t < 10; t++) {
if(digitCount[t]) {
printf("%d-----%d\n", t, digitCount[t]);
}
}
return 0;
}
輸出:
9 Digits
0-----2
1-----3
2-----1
5-----3
注意:輸入n
不得溢出Integer 。 要查找數字中的位數,您不需要floor
、 pow
或log10
,只需要/
和%
就足夠了。
您的 while 循環中存在錯誤。
while(floor(log10(n))-t)
應該
while(0 <= floor(log10(n))-t)
您的情況下的索引應該從 0 而不是 1 開始。
修復該錯誤,您應該得到正確的輸出,如下所示:
6 Digits
1-----0
2-----1
5-----2
5-----3
2-----4
1-----5
要找出為什么得到 7 而不是 5,您需要進行一些嚴肅的診斷打印——也許使用如下代碼:
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int n = 125521;
int d = floor(log10(n));
printf("n = %d: %d Digits\n", n, d + 1);
int t = 0;
while (floor(log10(n)) >= t)
{
printf("t = %d: n = %d; L = log10(n) = %f; F = floor(L) = %f\n",
t, n, log10(n), floor(log10(n)));
printf(" T = F-t = %f;", floor(log10(n)) - t);
printf(" P = pow(10,T)= %f\n", pow(10, floor(log10(n)) - t));
printf(" I = (int)P = %d; D = n/I = %d; M = D %% 10 = %d\n",
(int)pow(10, floor(log10(n)) - t),
n / (int)pow(10, floor(log10(n)) - t),
n / (int)pow(10, floor(log10(n)) - t) % 10);
printf("%d-----%d\n", (n / (int)pow(10, floor(log10(n)) - t) % 10), t);
t++;
}
return 0;
}
我修改了循環條件; 舊式 Fortran II 算術if
條件在 C 中確實不是很好的樣式。它還打印最后一位數字。 如果循環是for (int t = 0; t <= d; t++)
編寫的for (int t = 0; t <= d; t++)
那會更好,但我沒有做那個改變。
在運行 OS X 10.10.3 的 Mac 上,使用 GCC 5.1.0 編譯 64 位程序,結果是:
n = 125521: 6 Digits
t = 0: n = 125521; L = log10(n) = 5.098716; F = floor(L) = 5.000000
T = F-t = 5.000000; P = pow(10,T)= 100000.000000
I = (int)P = 100000; D = n/I = 1; M = D % 10 = 1
1-----0
t = 1: n = 125521; L = log10(n) = 5.098716; F = floor(L) = 5.000000
T = F-t = 4.000000; P = pow(10,T)= 10000.000000
I = (int)P = 10000; D = n/I = 12; M = D % 10 = 2
2-----1
t = 2: n = 125521; L = log10(n) = 5.098716; F = floor(L) = 5.000000
T = F-t = 3.000000; P = pow(10,T)= 1000.000000
I = (int)P = 1000; D = n/I = 125; M = D % 10 = 5
5-----2
t = 3: n = 125521; L = log10(n) = 5.098716; F = floor(L) = 5.000000
T = F-t = 2.000000; P = pow(10,T)= 100.000000
I = (int)P = 100; D = n/I = 1255; M = D % 10 = 5
5-----3
t = 4: n = 125521; L = log10(n) = 5.098716; F = floor(L) = 5.000000
T = F-t = 1.000000; P = pow(10,T)= 10.000000
I = (int)P = 10; D = n/I = 12552; M = D % 10 = 2
2-----4
t = 5: n = 125521; L = log10(n) = 5.098716; F = floor(L) = 5.000000
T = F-t = 0.000000; P = pow(10,T)= 1.000000
I = (int)P = 1; D = n/I = 125521; M = D % 10 = 1
1-----5
您應該運行此程序或類似的程序並顯示中間計算的結果。 然后我們也許可以開始理解為什么你看到的是 7 而不是 5。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.