- //求出四位数中的个位+百位=十位+千位{3553,2000,2332,1166,3000,8228,5000,3773,8000,8888,6666}中的数字的个数,
- //并按照倒序(从大到小)输出所有符合条件的数.
- //思路: 1.将数组从大到小重新排序
- // 2.查找出符合条件的数字并输出
- class HomeWork2 {
- public static void main(String[] args){
- int [] arr = {3553,2000,2332,1166,3000,8228,5000,3773,8000,8888,6666};
- arr = paixu(arr);
- for (int i = 0;i < arr.length ;i++ ) {
- int ge = arr[i] % 10;
- int shi = arr[i] / 10 % 10;
- int bai = arr[i] / 100 % 10;
- int qian = arr[i] / 1000;
- if (ge + bai == shi + qian) {
- System.out.println(arr[i]);
- }
- }
- }
- public static int [] paixu(int [] arr){//对数组进行从大到小的顺序排序
- for (int i = 0;i < arr.length ;i++ ) {
- for (int j = i;j < arr.length ;j++ ) {
- int temp = 0;
- if (arr[i] < arr[j]) {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- return arr;
- }
- }
复制代码
|
|