如何打印数组的偶数和/平均值和奇数和/平均值?

时间:2019-04-16 01:18:53

标签: swift

var a = [32,37,68,66,97,38,16,86,35,58]

我想知道, 如何打印偶数和和平均数,奇数和和平均数

3 个答案:

答案 0 :(得分:0)

添加这样的扩展名

void new_sort(int nums[], int count) {
 int round; 
 int i;
 int inorder;

  int temp;
  int *num_ptr = nums;

  inorder=0;

  for (round = count -1; (round>0)&&(!inorder); round--) {
     inorder=1;
     for(i=0; i<round; i++) {
        if (*num_ptr<*(num_ptr+1)) {
           inorder = 0;
           temp = *num_ptr;
           *num_ptr = *(num_ptr+1);
           *(num_ptr+1) = temp;
        }
    }
  }
}

然后执行此操作:

extension Sequence where Self.Element == Int {
    var sum: Int {
        return self.reduce(0, { $0 + $1 })
    }
}

答案 1 :(得分:0)

没有扩展名,可能会让初学者更容易理解,这是我的操作方法:

let oddElements = a.filter{$0 % 2 == 0}
let evenElements = a.filter{$0 % 2 != 0}

let oddElementsSum = oddElements.reduce(0, {$0 + $1})
let oddElementsAverage = oddElementsSum / oddElements.count

let evenElementsSum = evenElements.reduce(0, {$0 + $1})
let evenElementsAverage = evenElementsSum / evenElements.count

可以通过以下方式使reduce语句更短

let oddElementsSum = oddElements.reduce(0, {+})
let evenElementsSum = evenElements.reduce(0, {+})

然后您可以通过

打印
print (oddElementsSum)
print (oddElementsAverage)
print (evenElementsSum)
print (evenElementsAverage)

答案 2 :(得分:0)

O( n

您可以一次完成:

let (oddsSum, oddsCount, evensSum, evensCount) = a.reduce(into:(0, 0, 0, 0)) { tuple, element in
    if element % 2 != 0 {
        tuple.0 += element
        tuple.1 += 1
    } else {
        tuple.2 += element
        tuple.3 += 1
    }
}

let oddsAverage = oddsCount == 0 ? 0.0 : Double(oddsSum) / Double(oddsCount)
let evensAverage = evensCount == 0 ? 0.0 : Double(evensSum) / Double(evensCount) 

这将产生以下结果:

oddsSum       //169 
oddsAverage   //56.33333333333334 
evensSum      //364 
evensAverage  //52

两个数组

您可以像这样创建两个赔率数组:

let a = [32, 37, 68, 66, 97, 38, 16, 86, 35, 58]

let (odds, evens) = a.reduce(into:([Int](), [Int]())) { tuple, element in
    if element % 2 != 0 {
        tuple.0.append(element)
    } else {
        tuple.1.append(element)
    }
}

oddsevens是两个整数数组:[Int]


总和

要计算总和:

let oddsSum = odds.reduce(0, +)           //169
let evensSum = odds.reduce(0, +)          //364

您还可以使用Accelerate框架中的cblas_dasum(_:_:_:),它比reduce快:

import Accelerate

var (odds, evens) = a.reduce(into:([Double](), [Double]())) { tuple, element in
    if element % 2 != 0 {
        tuple.0.append(Double(element))
    } else {
        tuple.1.append(Double(element))
    }
}

var oddSum = Int(cblas_dasum(Int32(odds.count), &odds, 1))
print(oddSum)     //169

var evenSum = Int(cblas_dasum(Int32(evens.count), &evens, 1))
print(evenSum)    //364

平均值

平均值是总和除以每个数组中的元素数。

let oddsAverage = odds.isEmpty ? 0.0 : Double(oddsSum) / Double(odds.count)    //56.33333333333334
let evensAverage = evens.isEmpty ? 0.0 : Double(evensSum) / Double(evens.count) //52

或者您可以使用Accelerate框架中的vDSP_meanvD(_:_:_:_:),它比计算总和然后除以计数要快:

import Accelerate

var (odds, evens) = a.reduce(into:([Double](), [Double]())) { tuple, element in
    if element % 2 != 0 {
        tuple.0.append(Double(element))
    } else {
        tuple.1.append(Double(element))
    }
}

var oddAvg = 0.0
vDSP_meanvD(&odds, 1, &oddAvg, vDSP_Length(odds.count))
print(oddAvg)     //56.333333333333336

var evenAvg = 0.0
vDSP_meanvD(&evens, 1, &evenAvg, vDSP_Length(evens.count))
print(evenAvg)    //52.0