1. 程式人生 > >各位數字之和排序

各位數字之和排序

import java.util.*;

public class Main {
	public static class point {
		public int x;
		public int sum;
		public int order;
	}

	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		point a[] = new point[101];
		point t;
		int i, j, n, y,z;
		while (input.hasNext()) {
			n = input.nextInt();
			for (i = 1; i <= n; i++) {
				a[i] = new point();
				a[i].x = input.nextInt();
				a[i].order = i;
				z=a[i].x;
				while (z != 0) {
					y = z % 10;
					z /= 10;
					a[i].sum += y;
				}

			}
			for (i = 1; i <= n - 1; i++)
				for (j = 1; j <= n - i; j++)
					if (a[j].sum > a[j + 1].sum) {
						t = a[j];
						a[j] = a[j + 1];
						a[j + 1] = t;
					}
			for (i = 1; i < n; i++)
				System.out.print(a[i].x+ " ");
			System.out.println(a[n].x);
		

		}	//input.close();

	}

}