得到~对标准输出没有回应~在黑客排名问题上
getting ~ no response on stdout ~ on hackerrank problem
我在HackerRank上为一个简单的问题编写了代码,你必须计算一个数字的位数,然后将数字除以0余数。
我使用了一个基本的 while 循环来测试每个数字。测试用例的数量为 t,数量为 n。
我写的代码是:
#include <bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
int t;
cin >> t;
int count[t];
for (int t_itr = 0; t_itr < t; t_itr++) {
int n;
cin >> n;
int dig,temp=n;
count[t_itr]=0;
while(n>0){
dig=n%10;
if(temp%dig==0){
count[t_itr]++;
}
n=n/10;
}
}
for(int x=0;x<t;x++){
cout<<count[x]<<endl;
}
return 0;
}
输入:
2
12
1012
预期输出:
2
3
我的输出:
~ no response on stdout ~
正如@RetiredNinja所指出的,问题在于您正在调用未定义的行为:
C++标准(2003(在§5.6/4中说,
[...]如果/或 % 的第二个操作数为零,则行为未定义;[...]
也就是说,以下表达式调用未定义的行为 (UB(:
X/0;//UB X % 0;UB
未定义的行为,如果您不知道:
未定义的行为 - 对程序的行为没有限制。
换句话说,如果您的程序中包含未定义的行为,则C++对程序中发生的情况没有任何限制。您的程序实际上可以做任何事情,包括但不限于不std::cout
任何东西。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 需要帮助调试"attribute parser"!C++ 来自黑客排名的问题
- 这是一个二叉搜索树吗?黑客排名问题
- 在黑客地球上没有获得以下C ++代码问题的输出
- 在C++中解决最小掉期黑客排名问题
- 优化:黑客地球邮递员软件工程师实习生问题
- 得到~对标准输出没有回应~在黑客排名问题上
- 为什么在黑客兰克的许多问题中都会完成拆分字符串
- 黑客排名最小最大值问题,最大总和给出错误的负值
- 如何修复问题中的"teminated due to timeout"错误 数组操作 |黑客兰克?
- 围绕黑客排名的问题 30 天代码,第 6 天 [C++]
- 黑客世界,霍格沃茨的女巫们.我的方法有什么问题?