25.一个四位正整数M满足千位上的数字与百位上的数字之和为9,且十位上的数字与个位上的数字之和 为5,则称M为“九五数”,将“九五数”M的千位上的数字与十位上的数字交换、百位上的数字与个位上的数字交换得到一个新的四位正整数M,则称这个数M为M的“九五数”。判断7214,3550是否是“九五数”,用java程序实现
时间: 2023-03-21 16:01:47 浏览: 73
7214 不是“九五数”,因为它的千位数字和十位数字之和不等于 9,而是等于 7。而 3550 是“九五数”,因为它的千位数字和十位数字之和为 5,百位数字和个位数字之和也为 5。
以下是用 Java 实现的代码:
```
public class Main {
public static void main(String[] args) {
int num1 = 7214;
int num2 = 3550;
if (isNineFiveNumber(num1)) {
System.out.println(num1 + " 是“九五数”");
} else {
System.out.println(num1 + " 不是“九五数”");
}
if (isNineFiveNumber(num2)) {
System.out.println(num2 + " 是“九五数”");
} else {
System.out.println(num2 + " 不是“九五数”");
}
}
public static boolean isNineFiveNumber(int num) {
int thousand = num / 1000;
int hundred = num % 1000 / 100;
int ten = num % 100 / 10;
int one = num % 10;
if (thousand + hundred == 9 && ten + one == 5) {
return true;
} else {
return false;
}
}
}
```
阅读全文
相关推荐

















