【发布时间】:2015-06-09 19:10:18
【问题描述】:
我在hackerrank,遇到了一个非常奇怪的问题。问题是“给你一个整数 N。找到这个数字中正好除以 N 的数字(除以 0 作为余数)并显示它们的计数。对于 N = 24,有 2 个数字(2 和 4)。两者这些数字中的 24 个正好除以 24。所以我们的答案是 2。”。这是我的解决方案:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t, count;
cin >> t;
for (int i = 0; i < t; i++) {
int n, copy;
cout << "i: " << i << " ";
cin >> n;
cout << "n: " << n << " ";
copy = n;
count = 0;
while (copy > 0) {
if (n % (copy % 10) == 0)
count++;
copy = copy/10;
}
cout << "\n" << count << "\n";
}
return 0;
}
t 是测试用例的数量,n 是要测试的数量。我可以毫无问题地将任何我想要的数字输入到 t 中。但是,如果我在 n 中输入一个带零的数字,则会出现浮点异常错误。错误发生在第二个 cout 语句之前。其他数字可以正常工作(例如,1024 会出错,但 1124 可以)。我在hackerrank网站上运行了这段代码。有人可以解释发生了什么吗?
【问题讨论】:
-
在使用前尝试打印出 n,你得到你的输入了吗?
-
@cehnehdeh,听说过调试吗?
-
@user35443 是的!现在使用 VS 2013,但我不假设发布者可以访问调试器。
-
@cehnehdeh 你没有得到讽刺,你......
-
This post 解释它。