Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 计算给定范围内重复数字的数字_Algorithm_Numbers - Fatal编程技术网

Algorithm 计算给定范围内重复数字的数字

Algorithm 计算给定范围内重复数字的数字,algorithm,numbers,Algorithm,Numbers,如何在不使用模函数的情况下找到具有重复数字的数字数(例如:11、101、15231:所有数字都有重复的1)。范围应该是用户定义的。有一些解决方案使用组合,但这是主观的预定范围。如果给出的范围是52和156,那么如何以最佳方式找到解决方案。假设您有一组数字 设A是维数为| | S | | x10的矩阵 int row = 0 for each n in S: let exp = log10(n) for(int i = exp; i >= 0; i--){ l

如何在不使用模函数的情况下找到具有重复数字的数字数(例如:11、101、15231:所有数字都有重复的1)。范围应该是用户定义的。有一些解决方案使用组合,但这是主观的预定范围。如果给出的范围是52和156,那么如何以最佳方式找到解决方案。

假设您有一组数字

设A是维数为| | S | | x10的矩阵

int row = 0
for each n in S:
    let exp = log10(n)
    for(int i = exp; i >= 0; i--){
        let division = n / pow(10, exp)
        A[row][ division ] = 1
        n -= division * pow(10, exp)
    }
    row++
}
使用此伪代码,您可以获得所有数字中存在的所有数字,而只需检查所有数字是否都有一些公共数字:

void check(matrix A){
    for(int i = 0; i < A.columns; i++){
        boolean all_equal = true
        for(int j = 0; j < A.rows; j++){
            if(A[i][j] != 1) all_equal = false
        }
        if(all_equal) print("all numbers have " + i + " digit in common")
    }
}
无效检查(矩阵A){
对于(int i=0;i
希望有帮助:)



我想您已经考虑了将数字转换为字符串的可能性,因此此解决方案不包括字符串,而不是尝试计算具有重复数字的数字的数量,它很容易计算每个位置具有唯一数字的数字的数量,并从所有可能的数字中减去它,以得到实际结果

# of numbers with repeating digits(R) = all possible numbers(A) - # of numbers with unique digits in each position(X)
现在,要在一定的[L,R]范围内计算
X
A
,我们可以先从
0
R
进行计数,然后将
0
减去
L-1

ans[L, R] = ans[0, R] - ans[0, L - 1]

我现在没有给出代码示例。给你的想法添点吃的,让我知道你需要更多解释的地方。

对于整数x%y和x-x/y*y是一样的。