[英]Error in extracting digits from integer
I have a simple program which gives the wrong output, the expected output is the digits of the number.我有一个简单的程序,它给出了错误的输出,预期的输出是数字的数字。
#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;
}
This gives the output这给出了输出
6 Digits
1-----0
2-----1
5-----2
7-----3
2-----4
Strange output.奇怪的输出。 Why does 7 come ?
为什么7来了?
I know how to get the digits by other ways but I want this solution to work.我知道如何通过其他方式获取数字,但我希望这个解决方案起作用。
Now as suggested in the answers I get rid of the bug in the while loop (>=0) and I get the output:现在,正如答案中所建议的,我摆脱了 while 循环中的错误 (>=0),并得到了输出:
Put proper spaces (n/(int)pow(10,floor(log10(n))-t)%10)
in this line.在此行中放置适当的空格
(n/(int)pow(10,floor(log10(n))-t)%10)
。
So many redundant call of floor(log10(n))
.这么多多余的
floor(log10(n))
调用。
while loop
will loop less than 1
from digit numbers. while loop
将从数字中循环小于1
。
I have no idea, why you print t
besides digits, say:我不知道,为什么除了数字之外还要打印
t
,请说:
1-----0
2-----1
^^^
Can't reproduce what you said.无法重现你所说的。
Are you looking for this:你在找这个吗:
#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;
}
Output:输出:
9 Digits
0-----2
1-----3
2-----1
5-----3
Caution: input n
must not overflow Integer .注意:输入
n
不得溢出Integer 。 To find the digit counts in a number, you do not need floor
, pow
or log10
, only /
and %
is sufficient.要查找数字中的位数,您不需要
floor
、 pow
或log10
,只需要/
和%
就足够了。
There is a bug in your while loop.您的 while 循环中存在错误。
while(floor(log10(n))-t)
Should be应该
while(0 <= floor(log10(n))-t)
The index in your case should start from 0 rather than 1.您的情况下的索引应该从 0 而不是 1 开始。
Fix that bug and you should get the proper output as follows:修复该错误,您应该得到正确的输出,如下所示:
6 Digits
1-----0
2-----1
5-----2
5-----3
2-----4
1-----5
To find out why you are getting the 7 instead of the 5, you need to do some serious diagnostic printing — maybe using code something like this:要找出为什么得到 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;
}
I've modified the loop condition;我修改了循环条件; the old-style Fortran II arithmetic
if
condition really isn't good style in C. It also prints the last digit.旧式 Fortran II 算术
if
条件在 C 中确实不是很好的样式。它还打印最后一位数字。 It would be better if the loop were written for (int t = 0; t <= d; t++)
, but I've not made that change.如果循环是
for (int t = 0; t <= d; t++)
编写的for (int t = 0; t <= d; t++)
那会更好,但我没有做那个改变。
On a Mac running OS X 10.10.3, using GCC 5.1.0 compiling a 64-bit program, the result is:在运行 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
You should run either this program or something very similar and show the results of the intermediate calculations.您应该运行此程序或类似的程序并显示中间计算的结果。 We can then, maybe, begin to understand why you see a 7 instead of a 5.
然后我们也许可以开始理解为什么你看到的是 7 而不是 5。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.