Tag: 有理数

简化Java中的分数

我的任务是发展理性课程。 如果500和1000是我的输入,则(½)必须是我的输出。 我自己写了一个程序来找到它。 还有另一种找到解决方案的最佳方法,或者我的程序已经是最好的了吗? public class Rational { public static void main(String[] args){ int n1 = Integer.parseInt(args[0]); int n2 = Integer.parseInt(args[1]); int temp1 = n1; int temp2 = n2; while (n1 != n2){ if(n1 > n2) n1 = n1 – n2; else n2 = n2 – n1; } int n3 = temp1 / n1 ; […]