分析以下需求,并用代码实现:
(1)按照从大到小的顺序输出四位数中的个位+百位=十位+千位(例如:3553,2332,1166,8228,3773)的数字及个数
(2)每行输出5个满足条件的数,之间用空格分隔 (3)如:9999 9988 9977 9966 9955
代码如下:
public class Work7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
int i = 9999;
while (i > 999) {
int a = i % 10;
// System.out.println("个位:"+ a);
int b = i / 10 % 10;
// System.out.println("十位:"+ b);
int c = i / 100 % 10;
// System.out.println("百位:"+ c);
int d = i / 1000;
// System.out.println("千位:"+ d);
if (a + c == b + d) {
count++;
System.out.print(i + " ");
if (count % 5 == 0) {
System.out.println();
}
}
i--;
}
System.out.println("个数为:" + count);
}
}
结果为: