得到~对标准输出没有回应~在黑客排名问题上

getting ~ no response on stdout ~ on hackerrank problem

本文关键字:黑客 问题 回应 标准输出 得到      更新时间:2023-10-16

我在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任何东西。