从给定数组中打印所有可能的子数组及其各自的总和,并以最大总和打印子数组。 [英] Print all possible sub-arrays from the given array and their respective sums and also print the sub-array with maximum sum.?

查看:67
本文介绍了从给定数组中打印所有可能的子数组及其各自的总和,并以最大总和打印子数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ex:1,2,3

1

2

3

1,2-- 3

1,3--4

2,3--5

最大子阵列是2,3--5
..

这是我的代码。我有输出。但任何人都可以给出简单的逻辑来解决这个问题吗?

ex: 1, 2 ,3
1
2
3
1,2--3
1,3--4
2,3--5
max sub array is 2,3--5
..
here is my code. i got output. but can any one give simple logic to solve this?

#include < stdio.h >
#include < conio.h >
void main()
{
  int a[ 20], count = 1, m, copy[20], maxarry[20], n, x, i, j, noe, sum, l, max;
  clrscr();
  printf("\n ENter the no of elements.");
  scanf("%d", &n);
  printf("\n Enter array");
  for (i = 0; i < n; i++)>
    scanf( "%d" ,&a[i]);
  for (i = 0; i < n; i++)>
  {
    copy[i]=a[i];
  }
  for (i = 0; i < n; i++)>
    printf("%d\n", a[i]);
  //noe ia no of elemnts in subarray
  noe=2;
  max=0;
  //possible sub arrays is minimum 2 elements maximum n-1 elements so till noe value < n form sub arrays
  while(noe < n)>
  {
    for (i = 0; i < n; i++)>
    {
      /**take the element  a[i] and find possible combinations with copy[j]
      a[] = 1 2 3 c[] = 1 2 3 j = i + 1, with a[i] and find possible sub arrays from copy[i+1]*/
      for (j = i + 1; j < n; j++)>
      {
        count = 1;
        //check the possibility of an index j to form sub array
        if ((n - j) >= (noe - 1))
        {
          printf("%d,", a[i]);
          x = j;
          sum = a[i];
          while (count < noe)>
          {
            count++;
            printf("%d,", copy[x]);
            sum += copy[x];
            x++;
          }
          printf("---%d", sum);
          if (sum > max)
          {
            maxarry[0] = a[i];
            for (l = 1, m = j; m++)
              maxarry[l]=c[m];
              max = sub;
          }
          printf("\n");
        }
      }
    }
    k++;
  }
  printf("sub array with max sum");
  for (i = 0; i < l; i++)>
    printf("%d,", maxarray[i]);
  printf("-----%d", max);
  getch();
}



[edit]已添加代码块 - OriginalGriff [/ edit]

[edit2]重新添加代码块&以可读性为目的 - phil.o [/ edit2]


[edit]Code block added - OriginalGriff[/edit]
[edit2]Code block re-added & idented for readability purpose - phil.o[/edit2]

推荐答案

谢谢。同时更改程序中变量的名称。我来到知道我在两个不同的上下文中使用了一个变量名。现在我得到了输出。谢谢。

你能否给出任何其他想法来简单地编写这个程序




说真的,首先让缩进正确。哪个更容易阅读:

"thank you . while changing the name of the variables in my program. i came to know that i used a variable name in two different context. now i got the output. thank you.
and can you give any other idea to code this program in easy way"


Seriously, start by getting the indentation right. Which is easier to read:
for(i=0;i<n;i++)
{
/**take the element  a[i] and find
possible combinations with copy[j]
a[]= 1 2 3 c[]=1 2 3
j=i+1,  with a[i] and find
possible sub arrays from copy[i+1]*/
for(j=i+1;j<n;j++)
{
count=1;
//check the possibility of an index j to form sub array
if((n-j)>=(noe- 1 ))
{
printf("%d," ,a[i]);
x=j;
sum=a[i];
while(count<noe)
{
count++;
printf("%d," ,copy[x]);
sum+=copy[x];
x++;
}
printf("- - -%d" ,sum);
if(sum>max)                
{
maxarry[0]=a[i];                      
for(l=1,m=j;m++)
maxarry[l]=c[m];
max=sub;
}
printf("\n" );
}    }
    }
k++;
}



Or

for(i=0;i<n;i++)
    {
    /**take the element  a[i] and find
    possible combinations with copy[j]
    a[]= 1 2 3 c[]=1 2 3
    j=i+1,  with a[i] and find
    possible sub arrays from copy[i+1]*/
    for(j=i+1;j<n;j++)
        {
        count=1;
        //check the possibility of an index j to form sub array
        if((n-j)>=(noe- 1 ))
            {
            printf("%d," ,a[i]);
            x=j;
            sum=a[i];
            while(count<noe)
                {
                count++;
                printf("%d," ,copy[x]);
                sum+=copy[x];
                x++;
                }
            printf("- - -%d" ,sum);
            if(sum>max)                
                {
                maxarry[0]=a[i];                      
                for(l=1,m=j;m++)
                maxarry[l]=c[m];
                max=sub;
                }
            printf("\n" );
            }
        }
    }
k++;
}

使用第二个,您可以一目了然地看到控制流是什么,并且您有三个嵌套循环 - 从第一个开始您根本无法看到一,因为你必须花费大量的时间来手工匹配括号。



其次,总是使用描述性名称 - i,j,k,并且n并不意味着任何特别的东西,很容易让人感到困惑 - 正如你在试图改变它们时所看到的那样! :笑:这是很多,更容易使用真实的名字,从一开始就谈论变量是什么,因为你不必花时间记住你在哪一个。


最后,这就是所谓的单片代码 - 它只是一个块,这意味着它更难理解。

尝试制作单独的函数:输入处理,输出。

然后将处理分成几部分:生成所有可能的数组,对数组进行排序。



这样任务变得更简单,更容易理解和编码。对于像这样的琐碎项目来说,这看起来有点过分,但这是一个好主意,因为它是你在现实世界中必须这样做的方式,它确实让你更容易专注于各种任务,并获得每个一个就在你进入下一个之前。

尝试一下:它并不困难,它让生活更轻松。 (备份你已经得到的东西,然后尝试一下 - 如果你感到非常困惑,你仍然有东西交给你的导师!:笑:):





我讨厌该死的降价!代码中的删除线?加油...... [/ edit]

With the second one, you can see at a glance what the flow of control is, and that you have three nested loops - you can't see that at all easily from the first one, because you have to spend loads of time matching brackets by hand.

Second, always use descriptive names - "i", "j", "k", and "n" don't mean anything in particular and it is horribly easy to get confused - as you have seen when you tried to change them! :laugh: It's a lot, lot easier to work with "real" names that talk about what the variable is there for from the start, because you don't have to spend time remembering which is which as you go along.

Finally, that's whats called "monolithic code" - it's all one block, and that means it's harder to understand.
Try making separate functions: input, processing, output.
Then break processing into parts: Generate all possible arrays, sort the arrays.

This way the tasks become simpler, and easier to understand and code. It may seem a little overkill for a trivial project like this, but it's a good idea, because it is the way you have to do it in the real world and it really does make it easier to focus on the various tasks, and get each one right before you move on to the next.
Try it: it's not difficult, and it makes life easier. (Backup what you've got, then give it a try - that way if you get horribly confused you've still got something to hand in to your tutor! :laugh: )


[edit]I hate damn markdown! Strikethrough in code? Come on...[/edit]


这篇关于从给定数组中打印所有可能的子数组及其各自的总和,并以最大总和打印子数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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