计算最小绝对值---c实现

#include <iostream>
#include "math.h"

using namespace std;

#define N 32
#define MAXINPUT 100

void sort(int input[]);
void calcmin(int input[],int *output);


void calc(int input[N][MAXINPUT],int n)
{
 int i=0;
 int output=0;
 for(i=0;i<n;i++)
 {
  sort(input[i]);
  calcmin(input[i],&output);
  cout<<output<<endl;
 }
 
}
int main()
{
 int input[N][MAXINPUT];
 int n;//输入的组数
 cin>>n;
 for(int i =0;i<n;i++)
 {
  cin>>input[i][0];
  for(int j=1;j<=input[i][0];j++)
  {
   cin>>input[i][j];
  }
 }
 calc(input,n);
 getchar();
 return 0;
}

 

void sort(int input[])
{
 int index1=0;
 int index2=0;
 int temp=0;
 for(index1=1;index1<=input[0];index1++)
 {
  for(index2=index1+1; index2<=input[0]; index2++)
  {
   if(input[index1]>input[index2])
   {
    temp= input[index1];
    input[index1]=input[index2];
    input[index2]=temp;
   }
  }
 }
}
void calcmin(int input[],int *output)
{
 int rslt = 0;
 int temp1=0;
 int temp2=0;
 int max=0;
 int divi = 0;
 int remain = 0;
 int i =0;
 int kk=0;

 rslt = int(input[0]/2);
 max= input[0];
 if(max%2)
 {
  //奇数
  for(i=1;i<=rslt;i++)
  {
   temp1 = temp1+input[max-i+1]*pow(10,rslt-i);
   temp2 = temp2+input[i]*pow(10,rslt-i+1);
  }
  temp2= temp2+input[rslt+1];
  if(input[1]==0)
  {
   kk=pow(10,rslt-1);
   remain=temp2%kk;
   divi = temp2/(pow(10,rslt-1));
   temp2= divi*pow(10,rslt)+remain;
  }
  
 }
 else
 {
  //偶数
  temp1 = input[rslt]*pow(10,rslt-1);
  temp2 = input[rslt+1]*pow(10,rslt-1);
  for(i=1;i<rslt;i++)
  {
   temp1=temp1+input[max+1-i]*pow(10,rslt-1-i);
   temp2=temp2+input[i]*pow(10,rslt-1-i);
  }
 }
 *output=temp2-temp1;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值