简体   繁体   English

为什么 x *= (y1*y2*y3)/z1 没有给出与 JAVA 中的 x = x * (y1*y2*y3)/z1 相同的答案

[英]Why is x *= (y1*y2*y3)/z1 not giving the same answer as x = x * (y1*y2*y3)/z1 in JAVA

I am solving this on Leetcode and I ran across this error while submitting the answer for the question.我在 Leetcode 上解决了这个问题,在提交问题的答案时遇到了这个错误。

ans *= (N-i+1)/i; is not the same as ans = ans * (N-i+1)/i;不等于ans = ans * (N-i+1)/i;

The value of answer for each iteration m=51 and n=9每次迭代m=51n=9的 answer 值

  1. For ans *= (N-i+1)/i;对于ans *= (N-i+1)/i;
public int uniquePaths(int m, int n) 
    {
        double ans = 1;
        int N = m+n-2;
        int R = Math.min(n, m)-1;
        for(int i = 1; i <= R; i++)
        {
            ans *= (N-i+1)/i;
        }
        return (int)ans;
    }

The output for that is: output 是:

51/1
ans=58.0
52/2
ans=1624.0
53/3
ans=29232.0
54/4
ans=380016.0
55/5
ans=3800160.0
56/6
ans=3.040128E7
57/7
ans=2.1280896E8
58/8
ans=1.27685376E9
  1. For ans = ans * (N-i+1)/i;对于ans = ans * (N-i+1)/i;
public int uniquePaths(int m, int n) 
    {
        double ans = 1;
        int N = m+n-2;
        int R = Math.min(n, m)-1;
        for(int i = 1; i <= R; i++)
        {
            System.out.println((N-R+i)+"/"+i);
            ans = ans * (N-i+1)/i;
            System.out.println("ans="+ans);
        }
        return (int)ans;
    }
51/1
ans=58.0
52/2
ans=1653.0
53/3
ans=30856.0
54/4
ans=424270.0
55/5
ans=4582116.0
56/6
ans=4.0475358E7
57/7
ans=3.00674088E8
58/8
ans=1.916797311E9

When the values being multiplied are the same, why am I getting two different answers?当相乘的值相同时,为什么我会得到两个不同的答案?

In ans *= (N-i+1)/i;ans *= (N-i+1)/i; , (N-i+1)/i is evaluated first. , (N-i+1)/i首先被评估。 All the operands are int s, so integer division with truncation is performed.所有的操作数都是int s,所以执行了 integer 除法和截断。 The truncated result is then used to update the value of ans .然后使用截断的结果来更新ans的值。

With ans = ans * (N-i+1)/i;ans = ans * (N-i+1)/i; , the first operand to the multiplication operator is a double , so the result is a double. ,乘法运算符的第一个操作数是double ,所以结果是 double 。 That result is divided by i and floating-point division is performed.该结果除以i并执行浮点除法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重新格式化 GetVertices 以返回 (x1,y1,0), (x2,y2,0), (x3,y3,0);? - How to re format GetVertices to return (x1,y1,0), (x2,y2,0), (x3,y3,0);? 如何使用Quartz库每天在XX:XX处安排从日期X1 / Y1 / Z1到日期X2 / Y2 / Z2的某些任务? - How can I use Quartz library to schedule some task from date X1/Y1/Z1 to date X2/Y2/Z2 at XX:XX every day? 给定两个点 A(x1,y1) &amp; B(x2,y2),我想在球面上找到第三点 C(x3,y3) 和线 AB 之间的距离和 AD 的长度 - Given two points A(x1,y1) & B(x2,y2), I want to find the distance between the third point C(x3,y3) and line AB and length of AD on spherical surface 按钮无法绘制具有坐标(x1,x2,y1,y2)的折线图 - Button not working to draw line graph having its coordinate (x1, x2, y1, y2) 如果我们知道距离x1,y1,y2,则计算x2 - Calculate x2 if we know distance, x1, y1, y2 如何划分字符串(x1,y1)(x2,y2)格式 - How to divide string (x1,y1)(x2,y2) format 在java中以给定速度以直线将对象从点(x1,y1)移动到点(x2,y2)的方法 - Method to move an object from point(x1,y1) to point(x2,y2) at a given speed in a straight line in java 如何确定百万数据点中的哪些点 (x,y) 位于矩形 (x1, x2, y1, y2) 所描述的区域内? - How to determine which points (x,y) out of million data points lie inside the area described by a rectangle (x1, x2, y1, y2)? 在X1 y1 X2 y2形式的10,000个点的文件中,如何检测至少四个正方形? 爪哇 - In a file of 10,000 points of the form X1 y1 X2 y2 ,,, How to detect at least 4 which form a square ? java 相当于java中的x, y in z - Equivalent of for x, y in z in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM