繁体   English   中英

数组中所有可能的总和以及组合的投影

[英]All possible sums in an array and the projection of the combinations

好吧,首先我是C ++编程的新手,其次我有一个要解决的问题似乎让我头疼。 就像这样 我有一个char[3][14] (我的代码中为user_in ),我从用户那里得到输入以填充它。 用户仅输入'1','x','2','*' 例如:

1**
1*2
**2
*x*
1**
1**
*x2
**2
**2
1x*
1**
*x*
**2
1**
1x2

然后,我将此数组转换为另一个int[3][14] (在我的代码中为user_in_tr )。 对于每一个*我把0 ,对于1我把1 ,对x ,我把2 ,对于2我把3 所以:

1**  100
1*2  103
**2  003
*x*  020
1**  100
1**  100
*x2  023
**2  003
**2  003
1x*  120
1**  100
*x*  020
**2  003
1**  100
1x2  123

我想做的是计算所有可能的总和,从每一行中取一个元素。 请注意,该元素不得为0。因此,无论哪里有0,我们都将其跳过。 在我的示例中,它将是:

First:  1+1+3+2+1+1+2+3+3+1+1+2+3+1+1=26
Second: 1+1+3+2+1+1+2+3+3+1+1+2+3+1+2=27
Third:  1+1+3+2+1+1+2+3+3+1+1+2+3+1+3=28
Fourth: 1+1+3+2+1+1+2+3+3+2+1+2+3+1+1=27

等等...

最大总和= 42(14x3),最小总和= 14(14x1)

我也想显示添加的数字。
我想出了一些代码,但是结果不是我期望的是我的代码:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>


using namespace System;
using namespace std;

int main(array<System::String ^> ^args)
{
    char user_in[3][14];
    int user_in_tr[3][14]
    int i,j;
    int count0,count1,count2,count3,count4,count5,count6,count7,count8,count9,count10,count11,count12,count13;
    int sum;

    for(i=0;i<14;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%c",user_in[j][i]);
        }
    }

    for(i=0;i<14;i++)
    {
        for(j=0;j<3;j++)
        {
            if(user_in[j][i]=='*')
            {
                user_in_tr[j][i]=0;
            }
            else if(user_in[j][i]=='1')
            {
                user_in_tr[j][i]=1;
            }
            else if(user_in[j][i]=='x')
            {
                user_in_tr[j][i]=2;
            }
            else
            {
                user_in_tr[j][i]=3;
            }
        }
    }

   for(count0=0;count0<3;count0++)
   {

     for(count1=0;count1<3;count1++)
     {

      for(count2=0;count2<3;count2++)
      {

       for(count3=0;count3<3;count3++)
       {

        for(count4=0;count4<3;count4++)
        {

         for(count5=0;count5<3;count5++)
         {

          for(count6=0;count6<3;count6++)
          {

           for(count7=0;count7<3;count7++)
           {

            for(count8=0;count8<3;count8++)
            {

             for(count9=0;count9<3;count9++)
             {

              for(count10=0;count10<3;count10++)
              {

               for(count11=0;count11<3;count11++)
               {

                for(count12=0;count12<3;count12++)
                {

                 for(count13=0;count13<3;count13++)
                 {
                  sum=0;

                  if(user_in[count0][0]!=0&&user_in[count1][1]!=0&&user_in[count2][2]!=0&&user_in[count3][3]!=0&&user_in[count4][4]!=0&&user_in[count5][5]!=0&&user_in[count6][6]!=0&&user_in[count7][7]!=0&&user_in[count8][8]!=0&&user_in[count9][9]!=0&&user_in[count10][10]!=0&&user_in[count11][11]!=0,user_in[count12][12]!=0&&user_in[count13][13]!=0)
                  {
                   sum=user_in[count0][0]+user_in[count1][1]+user_in[count2][2]+user_in[count3][3]+user_in[count4][4]+user_in[count5][5]+user_in[count6][6]+user_in[count7][7]+user_in[count8][8]+user_in[count9][9]+user_in[count10][10]+user_in[count11][11]+user_in[count12][12]+user_in[count13][13];

                   printf("%d",sum);

                   printf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d",user_in[count0][0],user_in[count1][1],user_in[count2][2],user_in[count3][3],user_in[count4][4],user_in[count5][5],user_in[count6][6],user_in[count7][7],user_in[count8][8],user_in[count9][9],user_in[count10][10],user_in[count11][11],user_in[count12][12],user_in[count13][13]+"\n");
                  }
                 }
                }
               }
              }
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }

   system("PAUSE");
   return 0;
}

您可能考虑解决问题的一种方法是使用递归。 尽管并非总是好的做法,但是对此问题的递归可以显着提高可读性(对于处理数据的循环,只有1个)。

#include <iostream>
#include <vector>

typedef std::vector<unsigned> Row;
typedef std::vector<Row> Data;

void load_user_data(Data& data);
void calculate_sums(unsigned value, Data::const_iterator begin,
        Data::const_iterator end);

int main(int argc, char** argv)
{
    Data data;

    load_user_data(data);

    calculate_sums(0, data.begin(), data.end());

    return 0;
}

void load_user_data(Data& data)
{
    // WARNING: Untested, but should read data from user
    /*for (unsigned i = 0; i < 14; ++i)
    {
        Row temp;
        for (unsigned j = 0; j < 3; ++j)
        {
            char input = '\0';

            while (input == '\0')
            {
                std::cin >> input;
                switch (input)
                {
                case '1':
                    temp.push_back(1);
                    break;
                case 'x':
                    temp.push_back(2);
                    break;
                case '2':
                    temp.push_back(3);
                    break;
                case '*':
                    break;
                default:
                    input = '\0';
                }
            }
        }

        data.push_back(temp);
    }*/

    // Because I'm slightly lazy, the code bellow loads your data
    // into the vector Data.  If you had C++11 you could use
    // initializer lists.  If you want to try the code above,
    // comment out the code bellow.

    Row t;

    t.clear(); t.push_back(1); data.push_back(t);
    t.clear(); t.push_back(1); t.push_back(3); data.push_back(t);
    t.clear(); t.push_back(3); data.push_back(t);
    t.clear(); t.push_back(2); data.push_back(t);
    t.clear(); t.push_back(1); data.push_back(t);
    t.clear(); t.push_back(1); data.push_back(t);
    t.clear(); t.push_back(2); t.push_back(3); data.push_back(t);
    t.clear(); t.push_back(3); data.push_back(t);
    t.clear(); t.push_back(3); data.push_back(t);
    t.clear(); t.push_back(1); t.push_back(2); data.push_back(t);
    t.clear(); t.push_back(1); data.push_back(t);
    t.clear(); t.push_back(2); data.push_back(t);
    t.clear(); t.push_back(3); data.push_back(t);
    t.clear(); t.push_back(1); data.push_back(t);

    t.clear(); t.push_back(1); t.push_back(2); t.push_back(3);
    data.push_back(t);
}

void calculate_sums(unsigned value, Data::const_iterator begin,
        Data::const_iterator end)
{
    if (begin == end)
    {
        std::cout << value << std::endl;
        return;
    }

    for (Row::const_iterator entry = begin->begin();
            entry != begin->end(); ++entry)
    {
        calculate_sums(value + *entry, begin+1, end);
    }
}

如果要显示添加的数字,则可以将矢量替换为unsigned value ,并在每次迭代中将数字添加到其中,然后可以显示总和和分量。 如果有机会,我将在上面的代码中添加一个示例。

编辑:以下代码将输出全部和(包括数字和结果)。 与之前的代码唯一的不同是,您现在存储了组合,以便以后可以显示它们,这是通过将std::vector (列表)传递给函数calculate_sums (以前,我们只传递了运行总计值)。 希望这可以帮助。

void calculate_sums(const Row& values, Data::const_iterator begin,
        Data::const_iterator end);

int main(int argc, char** argv)
{
    Data data;

    load_user_data(data);

    Row temp;
    calculate_sums(temp, data.begin(), data.end());

    return 0;
}

void load_user_data(Data& data)
{
    // Same as above
}

void calculate_sums(const Row& values, Data::const_iterator begin,
        Data::const_iterator end)
{
    if (begin == end)
    {
        int value = 0;
        for (unsigned i = 0; i < values.size(); ++i)
        {
            value += values[i];
            std::cout << values[i] << char((i < values.size()-1) ? '+' : '=');
        }
        std::cout << value << std::endl;
        return;
    }

    for (Row::const_iterator entry = begin->begin();
            entry != begin->end(); ++entry)
    {
        Row temp(values);
        temp.push_back(*entry);
        calculate_sums(temp, begin+1, end);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM