/*************************************
本函数实现的功能是把:
数组,合并成一个最小的整数20,2,290,9
最后的结果是 2022909;
用到的算法有:
1,数值的转换方法
2,冒泡法排序
3,数值合并
************************************/
#include"stdio.h"
#include<string.h>
#include <math.h>
typedef struct st
{
char gewei;
char shiwei;
char baiwei;
}frend;
unsigned int chang_word(unsigned int date)
{
frend number;
int flag=0x00;
number.baiwei = date/100;
number.shiwei = date%100/10;
number.gewei = date%10;
if(number.baiwei) flag=0x4|flag;
if(number.shiwei) flag=0x2|flag;
if(number.gewei) flag=0x1|flag;
switch(flag)
{
case 0:
return 0;
case 1:
return number.gewei*100+number.gewei*10+number.gewei;
case 2:
case 3: return number.shiwei*100+number.shiwei*10+number.gewei;
default: return date;
}
}
main()
{
unsigned int a[4]={20,2,290,9};//结果是2022909
unsigned int b[4]={0};
unsigned int c[4]={0};
unsigned int tmpa;
unsigned int tmpb;
int i;
int j;
for(i=0;i<4;i++)
{
b[i]=chang_word(a[i]);//首先进行转换
printf("%d/n",b[i]);
}
printf("============/n");
for (i=0; i<4; i++)
{
for (j=0; j<(3-i); j++)
{
if(b[j]>b[j+1])
{
tmpb=b[j];
b[j]=b[j+1];
b[j+1]=tmpb;
tmpa=a[j];
a[j]=a[j+1];
a[j+1]=tmpa;
}
}
}
j=7;
for(i=0;i<4;i++)
{
if(a[i]>99)
{
j=j-3;
c[i]=a[i]*pow(10,j);
}
if((a[i]>9)&&(a[i]<100))
{
j=j-2;
c[i]=a[i]*pow(10,j);
}
if(a[i]<10)
{
j=j-1;
c[i]=a[i]*pow(10,j);
}
}
printf("result=%d/n",c[0]+c[1]+c[2]+c[3]);
}