标签: modulus

在 Python 中的递归中使用模数

新手尝试用 Python 中的递归函数将 int 的数字相加。当我使用 print 语句进行调试时,我可以看到我的程序正在计算除倒数第二个数字之外的所有数字。例如,如果我传入数字(12345),程序将计算5,然后跳过4,然后计算剩余的数字。任意数量的数字都会发生这种情况。我想知道为什么模数会隔离除倒数第二个数字之外的所有数字。(结果在其他方面也是不正确的,但我想重点关注模数如何递归运行。)

代码是:

def sumDigits(x):
    if x <= 0:
        return 1
    else:
        temp = x % 10
        x = x / 10
        print ('x = ', x, 'temp = ', temp)
        return temp + sumDigits(x -1)
Run Code Online (Sandbox Code Playgroud)

结果如下:

>>> sumDigits(5678)
('x = ', 567, 'temp = ', 8)
('x = ', 56, 'temp = ', 6)
('x = ', 5, 'temp = ', 5)
('x = ', 0, 'temp = ', 4)
24
>>> sumDigits(3456789)
('x …
Run Code Online (Sandbox Code Playgroud)

python modulus

1
推荐指数
1
解决办法
1061
查看次数

如何使用模数反转循环数组项

我正在编写一个反应组件,当单击前进handleClickLeft或后退 handleClickRight按钮时,它会向前或向后循环遍历数组。我通过使用模逻辑来做到这一点。我能够让前进按钮handleClickLeft正常工作,但我不知道如何反向操作handleClickRight。这是我的示例代码:

export default class Rooms extends React.Component {
   constructor(props, context) {
      super(props, context);
      this.state = {indexPos: [0, 1, 2]};
      this.state.itemArry = [{room1: 'this is room1'}, {room2: 'this is room2'}, {room3: 'this is room3'}, {room4: 'this is room4'}];

      this.handleClickLeft = this.handleClickLeft.bind(this);
      this.handleClickRight = this.handleClickRight.bind(this);
   }
   render() {   //Using index to show each item in the itemArry
      let firstItem = this.state.indexPos[0]
      let secondItem = this.state.indexPos[1]
      let thirdItem = this.state.indexPos[2]       
      <div>
         <ul>
           <li>this.state.itemArry[firstItem]</li>
           <li>this.state.itemArry[secondItem]</li>
           <li>this.state.itemArry[thirdItem]</li> …
Run Code Online (Sandbox Code Playgroud)

javascript arrays refactoring modulus reactjs

1
推荐指数
1
解决办法
1630
查看次数

使用数组索引和模运算符确定值

我讨厌数学。我可以在接下来的几个小时里坐在这里尝试解决这个问题,但我希望有半个大脑的人能够帮助我使用模数来解决这个基本的数学问题。

let bricks = [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]; // ...etc

for (var i = 0; i < bricks.length; i++) {
  // determine colour using modulus
  bricks[i].colour = help;
}
Run Code Online (Sandbox Code Playgroud)

我需要第一块砖是红色的,第二块砖是绿色的,第三块砖是蓝色的。然后对砖块数组中尽可能多的项目重复该模式。

这是一个有点尴尬的问题,但非常感谢任何帮助!

javascript math modulus

1
推荐指数
1
解决办法
1239
查看次数

Obj中奇怪的mod行为.C

我有以下代码:

NSInteger index1 = (stop.timeIndex - 1);  //This will be -1
index1 = index1 % [stop.schedule count];  // [stop.schedule count] = 33
Run Code Online (Sandbox Code Playgroud)

所以我的表达式为-1%33.这应该给我32,但反过来给我3 ...我已经仔细检查了调试器中的值.有没有人有任何想法?

c objective-c modulus

0
推荐指数
1
解决办法
632
查看次数

PHP版本的Javascript Modulus操作

我正在寻找Javascript模数(%)操作的PHP版本.我需要为我试图移植到PHP的一些映射算法得到这个.当我使用PHP的bcmod时,我的结果有些偏差.

这是我到目前为止所拥有的.

public static function mod($operand_str, $modulus_res)
{
    $arg_arr = array();
    $arg_arr = func_get_args();

    $operand_str = strval($operand_str);
    $modulus_res = strval($modulus_res);

    $retain_scale_bool = (!isset($arg_arr[2]) || $arg_arr[2] == '') ? false: $arg_arr[2];

    //get decimal
    $decimal_arr = array();
    $decimal_arr = explode('.', $operand_str);

    switch(true)
    {
        case ($retain_scale_bool == true):
            $modulus_new_res = bcmod($operand_str, $modulus_res);
            $modulus_new_res = $modulus_new_res.'.'.$decimal_arr[1];
        break;

        default:
            $modulus_new_res = bcmod($operand_str, $modulus_res);
    }

    return $modulus_new_res;
}
Run Code Online (Sandbox Code Playgroud)

仅作为一个例子.这是我得到的结果3.1432444%3:使用Javascript:0.14324439999999994 PHP:0 With My Function:0.1432444

我想用我的函数获取Javascript结果.

你能帮忙调整我的剧本吗?我不算数学,所以我不能把它带到模数运算的第一原理.

谢谢.

php modulus

0
推荐指数
1
解决办法
926
查看次数

请解释为什么17%40 = 17

我是Java的新手,实际上是编程.我知道模数运算符(%)返回两个数的余数,但是,我不明白为什么17%40 = 17.

据我所知,40%17 = 6,17%5 = 2,40%5 = 0.我得到了剩下的返回值的要点.但是17%40 = 17让我难过.

我可以设计的唯一合理化是,由于余数小于1,所以返回总值17,为什么不是0?请帮我解释这个谜.

java operators modulus

0
推荐指数
1
解决办法
585
查看次数

在Java中使用mod 3

所以,我想找到1到100之间的数字可以被3和7整除.我得到它的工作,除了其中一个数字.由于某种原因,3%3给了我3作为余数,但6%3给了我0.这是我的代码:

     public class factors
     {
public static void main(System args[])
{
    //Variables
    int integer, remainder;

    //Displays header
    System.out.print("Integers less than 100 that are \nevenly divisible by 3 or 7");

    //Loops through each integer
    for (integer = 1; integer <= 100; integer++)
    {
        remainder = integer % 3; //determines if 3 is a factor

        if (remainder == 0) //displays integer 
        {
            System.out.println(integer + " is divisible by 3");
        }

        remainder = integer % 7; //determines if 7 is a factor

        if …
Run Code Online (Sandbox Code Playgroud)

java modulus

0
推荐指数
2
解决办法
5539
查看次数

为什么0%0会导致1?

我的代码:

long long difft, intrv ;

cout << "difft = " << difft << endl;
cout << "intrv = " << intrv << endl;
cout << "difft mod intrv = " << difft%intrv << endl;
Run Code Online (Sandbox Code Playgroud)

输出:

 difft = 0
 intrv = 0
 difft mod intrv = 1
Run Code Online (Sandbox Code Playgroud)

0%0的结果为零,但在我的代码中结果为"1",为什么?

c++ undefined-behavior modulus

0
推荐指数
2
解决办法
143
查看次数

为什么第二个程序有效,但第一个无效?

第一:

a = int(input())
if a%4 == 0:
    a += 1
if a%4 != 0:
    a -= 1
print(a)
Run Code Online (Sandbox Code Playgroud)

第二:

a = int(input())
b = a%4
if b == 0:
    a += 1
if b != 0:
    a -= 1
print(a)
Run Code Online (Sandbox Code Playgroud)

python if-statement modulus

0
推荐指数
1
解决办法
49
查看次数

c中的%(模)运算符如何工作?

我正在尝试以下代码

printf("%d", 010 % 10);
Run Code Online (Sandbox Code Playgroud)

我期望它的输出是0,但它是8。

为什么?

c modulus

0
推荐指数
1
解决办法
57
查看次数