《編程思維與實(shí)踐》1030.按整數(shù)最高位的值排序
題目


思路
用字符串讀取,結(jié)構(gòu)體存取十進(jìn)制最高位數(shù)字和數(shù)本身的大小.
注意的點(diǎn):
整數(shù)范圍可能為10^18,應(yīng)該用longlong存儲(chǔ).
代碼
typedef?struct{int?firstnumber;long?long?number;}Data;
int?cmp(const?void?*a,const?void?*b)
{
????Data?*m=(Data*)a;
????Data?*n=(Data*)b;
????if(m->firstnumber!=n->firstnumber)
????{
????????return?n->firstnumber-m->firstnumber;
????}
????else{
????????if(m->number>n->number){
????????????return?1;
????????}
????????else{
????????????return?-1;
????????}
????}
}
int?main()
{
????int?T;
????scanf("%d",&T);
????for(int?i=0;i<T;i++)
????{
????????int?N;
????????scanf("%d",&N);
????????Data?tab[N];
????????char?s[N][100];?
????????for(int?j=0;j<N;j++)
????????{
????????????scanf("%s",s[j]);
????????????tab[j].number=atoll(s[j]);
????????????tab[j].firstnumber=s[j][0]=='-'?s[j][1]-'0':s[j][0]-'0';??//跳過(guò)負(fù)號(hào)
????????}???????????
????????qsort(tab,N,sizeof(Data),cmp);
????????printf("case?#%d:\n",i);
????????for(int?j=0;j<N;j++)
????????{
????????????printf("%lld?",tab[j].number);
????????}
????????printf("\n");
????}???
????return?0;
}