How do you implement tuples in Java for learning algorithms?
                                        
                    •
                    Java                                    
                I'm learning the algorithm by myself. If my title is incorrect, I'm sorry! I don't understand how to implement it in Java
if x = 0: return (q,r) = (0,0) (q,r) = divide(⌊x/2⌋,y) q=2·q,r=2·r if x is odd: r=r+1 if r≥y: r=r−y,q=q+1 return (q,r)
I don't know how to implement the following parts in Java
(q,r)=(0,0) (q,r)=divide(⌊x/2⌋,y) return (q,r)
Solution
Although Java lacks built-in support for tuples, the good news is that you don't have to use tuples to implement this algorithm except for return values Both regular int variables R and Q are OK
// (q,0) int q = 0,r = 0; // (q,y) q = (x/2) / y; r = (x/2) % y;
Returning is tricky because you have to return two values The usual way to use it in Java is to define a class:
class QandR {
    private final int q;
    private final int r;
    public QandR(int q,int r) {
        this.q = q;
        this.r = r;
    }
}
Now you can return the new qandr (Q, R) from the method
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        