排序并计算

/*************************************

本函数实现的功能是把:

数组,合并成一个最小的整数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]);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值