一、求兩個數的最大公約數與最小公倍數
package cn.lemon;
public class test05 {
public static void main(String[] args) {
int a = 5;
int b = 8;
int p = a * b;
while (b != 0) {
int s = a % b;
a = b;
b = s;
}
System.out.println("最大公約數爲:" + a);
int m = p / a;
System.out.println("最小公倍數爲:" + m);
}
}
二、將數組中兩兩相加之和爲 10 的元素輸出,已輸出的元素禁止再次使用。
注意事項:
- 只禁用已輸出的元素,其他與輸出元素值相同的元素不禁用
- 數組:
int[] arr = {1, 2, 1, 7, 8, 7, 9, 5, 3, 4, 8, 9, 6};
package cn.lemon;
public class test02 {
public static void main(String[] args) {
int[] arr = {1, 2, 1, 7, 8, 7, 9, 5, 3, 4, 8, 9, 6};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] == 10 && i != j) {//i != j 表示去掉 5 + 5 = 10 這項
System.out.println("數組下標爲:" + i + ",數組元素爲:" + arr[i] + "\t\t數組下標爲:" + j + ",數組元素爲:" + arr[j]);
arr[i] = 10;//用過的元素重新賦值,也就是讓這個元素與其他任何一個元素相加都不會等於10
arr[j] = 10;
}
}
}
}
}
三、求數組中的最大值與最小值
package cn.lemon;
public class test06 {
public static void main(String[] args) {
int[] arr = {16, 32, 12, 5, 9, 88};
int max = arr[0];
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
if (min > arr[i]) {
min = arr[i];
}
}
System.out.println("最大值爲:" + max);
System.out.println("最小值爲:" + min);
}
}