#15358: c answer


blackconqueror (boweichen)

學校 : 國立臺南第一高級中學
編號 : 58182
來源 : [220.132.250.41]
最後登入時間 :
2020-06-01 10:17:42
a225. 明明愛排列 | From: [140.114.197.199] | 發表日期 : 2018-09-30 13:59

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int n,i,j;
while(scanf("%d",&n)!=EOF){
int a[n];
for(i=0;i<n;i++) scanf("%d",&a[i]);
int bucket[n][10],log[10];
for(i=0;i<10;i++) log[i]=0;
for(i=0;i<n;i++){
bucket[log[a[i]%10]][a[i]%10]=a[i];
log[a[i]%10]++;
}
int count=0;
for(i=0;i<10;i++){
if(log[i]>=2){//big to small
int temp[log[i]];
int k,b;
for(k=0;k<log[i];k++) temp[k]=bucket[k][i];
for(k=log[i];k>1;k--){
for(b=0;b<k-1;b++){
if(temp[b]>temp[b+1]){
int g=temp[b+1];
temp[b+1]=temp[b];
temp[b]=g;
}
}
}
for(b=log[i]-1;b>=0;b--){
a[count]=temp[b];
count++;
}
}else if(log[i]==1){//output
a[count]=bucket[0][i];
count++;
}

}
for(i=0;i<n;i++) printf("%d ",a[i]);
printf("\n");
}
return 0;
}

 
ZeroJudge Forum