JavaStudy——0089:倒置排序

总时间限制: 1000ms 内存限制: 32767kB

描述
将一些整数按倒置值排序后输出. 所谓倒置,是指把整数各位倒过来构成一个新数,例如:13倒置成了31.

输入
第一行的整数N表示后面列出的组数。每组数的第一个整数n表示后面将有n个整数。(每组数据量不超80)
输出
将每组数按倒置值进行排序输出.其每组数的结果占一行.

样例输入

2
4 83 13 24 36
4 99 100 123 12345

样例输出

13 83 24 36
100 99 123 12345

Accepted代码

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        List<Integer> a=new ArrayList<Integer>();
        List<Integer> num=new ArrayList<Integer>();
        for(int i=0;i<n;i++) {
            int num1=in.nextInt();
            num.add(num1);
            int[] a1=new int[num1];
            int[] a2=new int[num1];
            int[] a3=new int[num1];
            for(int j=0;j<num1;j++)
                a1[j]=in.nextInt();
            for(int j=0;j<num1;j++) {
                if(a1[j]<0) {
                    String q=-a1[j]+"";
                    String q1=new StringBuffer(q).reverse().toString();
                    q="-"+q1;
                    a2[j]=Integer.parseInt(q);
                }
                else {
                    String q=a1[j]+"";
                    String q1=new StringBuffer(q).reverse().toString();
                    a2[j]=Integer.parseInt(q1);
                }
            }
            Arrays.sort(a2);
            for(int j=0;j<num1;j++) {
                int k;
                if(a1[j]<0) {
                    String q=-a1[j]+"";
                    String q1=new StringBuffer(q).reverse().toString();
                    q="-"+q1;
                    k=Integer.parseInt(q);
                }
                else {
                    String q=a1[j]+"";
                    String q1=new StringBuffer(q).reverse().toString();
                    k=Integer.parseInt(q1);
                }
                for(int w=0;w<a2.length;w++) {
                    if(k==a2[w]) a3[w]=a1[j];
                }
            }
            for(int j=0;j<a3.length;j++) {
                a.add(a3[j]);
            }
        }
        int index=0;
        for(int i=0;i<n;i++) {
            for(int j=0;j<num.get(i);j++) {
                if(j==num.get(i)-1)
                    System.out.println(a.get(index+j));
                else
                    System.out.print(a.get(index+j)+" ");
            }
            index+=num.get(i);
        }
        in.close();
    }
}

猜你喜欢

转载自blog.csdn.net/Alexander1216/article/details/84102151
今日推荐