1. 程式人生 > >1188-C語言實驗——各位數字之和排序

1188-C語言實驗——各位數字之和排序

Problem Description

給定n個正整數,根據各位數字之和從小到大進行排序。

Input

輸入資料有多組,每組資料佔一行,每行的第一個數正整數n,表示整數個數,後面接n個正整數。當n為0時,不作任何處理,輸入結束。n<=10

Output

輸出每組排序的結果。

Sample Input

3 230 59 110
5 199 220 108 235 120
0

Sample Output

110 230 59
120 220 108 235 199

原始碼:

#include <stdio.h>
int main()
{
    int
x,i,j,temp; while (scanf("%d",&x)!=EOF) { if(x==0) break; else{ int a[2][10],sum=0; for(i=0;i<x;i++) scanf("%d",&a[0][i]); for(i=0;i<x;i++){ temp=a[0][i]; while(temp>=1){ sum+
=temp%10; temp/=10; } a[1][i]=sum; sum=0; } for(i=1;i<x;i++) for(j=0;j<x-i;j++) if(a[1][j]>a[1][j+1]){ temp=a[1][j]; a[1
][j]=a[1][j+1]; a[1][j+1]=temp; temp=a[0][j]; a[0][j]=a[0][j+1]; a[0][j+1]=temp; } printf("%d",a[0][0]); for(i=1;i<x;i++) printf(" %d",a[0][i]); printf("\n"); } } return 0; }

說明:

使用了二維陣列和氣泡排序,較為複雜。應該還有更為簡單的程式碼。努力!