第三行的总和,并以使其总和匹配的方式对行进行除法 [英] sum value of a 3rd row and divide rows in such a way that their sum values matches

查看:134
本文介绍了第三行的总和,并以使其总和匹配的方式对行进行除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的文件,其中的行数为n,我希望将其总和(基于第3列)并相应地将行分配到3个不同的文件中(基于每个文件的总和)

I have a file as below with n number of rows, I want to total it's sum(based on 3rd column) and distribute rows accordingly in 3 different files(based on sum of each)

例如-如果我们将所有第3列的值相加,则总计为516,如果将其除以3,则为172.

For example- if we sum all the 3rd column values it's total is coming as 516 and if we divide it by 3 it is 172.

因此,我想向文件中添加一行,以使其不超过172个标记,与第二个文件相同,其余所有行应移至第三个文件.

So i want to add a rows to a file so it doesn't exceed 172 mark, same with the 2nd file and rest all rows should move to the third file.

  • 只需确保所有3个文件的和值都应匹配(小的差异就可以了)
    • 可能是应该从顶部开始检查值并继续搜索到末尾并尝试添加尽可能多的值(只需要确保总值不超过172标记(是总值的三分之一)即可.总金额))
    • 应该避免所有第三个值为0的行,最后将所有行分配到所有3个文件中,这样它们的行数也应该接近相等.
    • Just have to make sure sum value of all the 3 files should match(small difference is fine)
      • may be it should start checking the values from the top and keep on searching till the end and try to add as much it can(just have to make sure total value doesn't exceed the 172 mark(which is one third of the total sum))
      • It should avoid all the rows which has third value as 0 and at last distribute all of them amongst all the 3 files in such a way that their row count should also be somewhere near equal.

      这是对问题的补充-> sum第三行的值并相应地划分行"

      This is in addition to question-"sum value of a 3rd row and divide rows accordingly"

      输入文件

      a aa 0
      b ab 55
      c ac 17
      a dy 0
      y ae 12
      a dl 34
      a fk 45
      l ah 56
      o aj 76 
      l ai 19 
      q al 0
      d pl 64
      e ik 0
      f ll 0
      g dl 25 
      h fr 17
      i dd 23
      j we 27
      k rt 25
      l yt 0
      m tt 19
      

      预期产量

      文件1 总计(172)

      file1 Total (172)

      b ab 55
      c ac 17
      y ae 12
      a dl 34
      a fk 45
      m tt 9
      

      文件2 总计(168)

      file2 Total (168)

      l ah 56
      o aj 76 
      l ai 19 
      h fr 17
      

      文件3 总计(174)

      file3 Total (174)

      d pl 64
      g dl 25
      i dd 23
      j we 27
      k rt 35
      

      以后,应检查所有0列并将其划分,以使每个文件的总行数也可以有所平衡.

      Later it should check for all the 0 column and divide it in such a way that total rows of each file can also be balanced a bit.

      a aa 0
      a dy 0
      q al 0
      e ik 0
      f ll 0
      l yt 0
      

      所以最终输出将是

      file1

      b ab 55
      c ac 17
      y ae 12
      a dl 34
      a fk 45
      m tt 9
      a aa 0
      

      file2

      l ah 56
      o aj 76 
      l ai 19 
      h fr 17
      a dy 0
      q al 0
      e ik 0
      

      file3

      d pl 64
      g dl 25
      i dd 23
      j we 27
      k rt 35
      f ll 0
      l yt 0
      

      OP从评论中尝试:

      awk '{ L[nr++]=$0; sum+=$3 } END{ sumpf=sum/3; sum=0; file=1; for(i in L) { split(L[i],a); if ((sum+a[3])>sumpf && file<3) { file+=1; sum=0; }; print i, L[i] > "file" file; sum+=a[3]; } }' input

      推荐答案

      您可以遵循几种方案:

      场景1)

      1. 读取值并将它们添加到file1中,只要合计值低于要求的合计值

      1. Read values and add them to file1, as long as, total is below required total

      如果file1的值接近要求的总数,则将值添加到file2,直到达到要求的总数

      If Value of file1 is near required total, add values to file2, until a required total

      如果file2的值接近总要求,则开始将其余部分添加到file3.

      If value of file2 is near required total start adding the rest to file3.

      这将为您提供如下结果:

      This will get you a result like:

      文件1,文件2和文件3的总计分别为163、153、200

      With totals of respectively for file1, file2 and file 3: 163, 153, 200

      这可能无法满足需求,所以

      This might not satisfy the needs, so,

      场景2)

      1. 我们首先订购降序排列的物品,然后按照情况1进行订购是什么?

      我们最终得到:

      哇,这真让人不满意!我什至不算总数!

      Wow, this is even less satisfying! I will not even count the totals!

      现在由OP TrueEntertainer 来考虑另一种情况,其中三个表被更均匀地填充

      Now it's up to the OP TrueEntertainer to think of another scenario, where the three tables are filled more evenly.

      真的应该不要,要比上述两种情况更难找到更好的方法!

      It should really not be that hard to find a better way than the above 2 scenario's !!

      这篇关于第三行的总和,并以使其总和匹配的方式对行进行除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆