
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int T = reader.nextInt();
while (T-- > 0) {
int n = reader.nextInt();
int m = reader.nextInt();
double r = 0.0;
if (m > n) {
int t = m;
m = n;
n = t;
}
r = n * 1.0 / m;/// 大于等于1
int i = 1;
int j = 1;
double s = 0.0;
int c = 0;
while (i <= n && j <= m) {
s = i * 1.0 / j;
if (s > r) {
j++;
c++;
} else if (s == r) {
i++;
j++;
c++;
} else if (s < r) {
i++;
c++;
}
}
System.out.println(c);
}
reader.close();
}
}

本文深入解析了一道Java编程竞赛题目,通过一个具体的算法问题,展示了如何使用Java进行输入读取、条件判断和循环控制,以解决数学比例计算的问题。文章详细介绍了代码逻辑,包括变量初始化、条件判断和循环迭代过程,适合对算法和Java编程感兴趣的读者。
11万+

被折叠的 条评论
为什么被折叠?



