【问题标题】:Sum of elements in matrix矩阵中的元素之和
【发布时间】:2015-08-23 04:35:27
【问题描述】:

我有一个矩阵:

{
  {2, 3},
  {4, 1, 6},
  {7, 8}
}

我需要从每个数组中选择一个元素并返回总和。例如:

  • 2 + 4 + 7 = 13
  • 2 + 1 + 7 = 10
  • 2 + 6 + 7 = 15
  • 2 + 4 + 8 = 14
  • 2 + 1 + 8 = 11
  • 2 + 6 + 8 = 16
  • 3 + 4 + 7 = 14
  • 3 + 1 + 7 = 11
  • 3 + 6 + 7 = 16
  • 3 + 4 + 8 = 15
  • 3 + 1 + 8 = 12
  • 3 + 6 + 8 = 17

我是算法新手,不知道如何开始解决问题。我需要使用什么类型的算法?

【问题讨论】:

  • “最大”部分究竟是从哪里来的?
  • @user2357112 ups,我从问题中删除了max
  • 从您的解释中可以看出,您需要遍历所有数组(它不是真正的矩阵)并计算总和。
  • 你有一个锯齿状数组,而不是一个矩阵。

标签: algorithm matrix


【解决方案1】:

如果您不介意将您的(锯齿状)矩阵表示为 JSON 中的数组数组,那么以下 (a) 可以回答问题,并且 (b) 可能会建议使用其他编程语言的方法。为了 (b),我附加了“组合”的定义,它在 jq 1.5 中作为内置函数提供。

有关 jq 的更多信息,请参阅https://stedolan.github.io/jq

程序:

combinations | add

由于程序非常简单,因此可以在命令行上轻松调用。这是一个成绩单(其中 $ 表示 Mac 或 Linux 或类似环境):

$ jq 'combinations|add'
[[2, 3],
 [4, 1, 6],
 [7, 8] ]

输出:

13
10
15
14
11
16
14
15
11
12
16
17

附录:

def combinations:
  if length == 0 then []
  else .[0][] as $x
  | (.[1:] | combinations) as $y
  | [$x] +  $y
  end ;

在这里,“组合”产生一个流。在其他一些语言中,生成数组可能更容易。例如,在 ruby​​ 中:

def combinations(ary)
  if ary.length == 0
    [[]]
  else
    ans=[]
    ary[0].each { |x| combinations(ary[1..-1]).each {|y| ans << [x] + y } }
    ans
  end
end  

【讨论】:

  • 嗨!谢谢,是对的!但是,你能在 c++ 或 python、javascript 上显示等效的函数 combinations 吗?我不明白.[0][] as $x| (.[1:] | combinations) as $y
  • 在 ruby​​ 中:def combination(ary) if ary.length == 0 [[]] else ans=[] ary[0].each { |x|组合(ary[1..-1]).each {|y| ans [[1, 3], [1, 4], [2, 3] , [2, 4]]
  • @rel1x 希望这会有所帮助。
【解决方案2】:

这是java中的等效代码。

class Sum_matrix
{
 public static void main(String []args)
 {
  int a[][]={
   {2, 3},
   {4, 1, 6},
   {7, 8}
  };
  int sum,i,j,k;
  for(i=0;i<a[0].length;++i)
  {
      sum=0;
      sum+=a[0][i];
      sum_fn(1,a,sum);
  }
 }
 public static void sum_fn(int i,int [][]a,int sum)
 {
     if(i==a.length)
      System.out.println(sum);
     else
     {
         for(int j=0;j<a[i].length;++j)
         {
             sum_fn(i+1,a,sum+a[i][j]);
         }         
     }   
 }
}

【讨论】:

    【解决方案3】:

    这是R 一个班轮:

    rowSums(expand.grid(list(c(2,3), c(4,1,6), c(7,8))))
    

    加上一些解释:

    # first you define a list of vectors. 
    # Each vector contains numbers with which you want to obtain combination.
    
    lst = list(c(2,3), c(4,1,6), c(7,8))
    
    # then you generate all combinations using function expand.grid
    # this will return a dataframe, with each line having a different combination.
    
    df = expand.grid(lst)
    
    #   Var1 Var2 Var3
    #1     2    4    7
    #2     3    4    7
    #3     2    1    7
    #4     3    1    7
    #5     2    6    7
    #6     3    6    7
    #7     2    4    8
    #8     3    4    8
    #9     2    1    8
    #10    3    1    8
    #11    2    6    8
    #12    3    6    8
    
    # Finally you just apply rowSums to have the desired result:
    
    rowSums(df)
    #[1] 13 14 10 11 15 16 14 15 11 12 16 17
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多