【问题标题】:Finding the sum of elements of the largest sub array of increasing order in C?在C中找到最大的升序子数组的元素之和?
【发布时间】:2014-03-02 13:29:08
【问题描述】:

我有一个名为 sample.txt 的文件,其中包含 1000 个整数(正数和负数)。首先,我将文件复制到一个大小为 1000 的数组中(比如 a)。 我的目标是在 a 中找到最大的 子数组 并找到其中元素的总和。 如果数组的连续元素按递增顺序排列,则数组是子数组。例如在数组 {12,23,3,1,-56,2,4,6,45,49,1,2,-10} 中,子数组是 {-56,2 ,4,6,45,49}。 然后我需要计算这个子数组的元素之和。

下面是我尝试使用 C 程序解决问题的方法。 我是非CS专业的,本学期刚完成C编程。非常感谢您的帮助。

int sum(int a[],int i,int temp)
{
 int sum=0,j;
 for(j=i;j<i+temp;j++)
  sum+=a[j];
 printf("Sum = %d", sum); 
 return 0;
}

int main()
{
    FILE *f1;
    int a[1000],b[900];
    int number,i=-1,counter=0,temp=1,check=1,j;
    f1 = fopen("sample.txt","r");
    while (!feof (f1) && fscanf (f1, "%d", &number) && i++ < 1000 )// copying the file to an array
    a[i] = number;
    fclose(f1);
    for(i=1;i<1000;i++)
    {
     if(a[i-1]<a[i])
      counter++;
     else
     {
       if(counter>temp)
         temp=counter;
       counter=0;
      } 
    }
    temp++;
    printf("Temp= %d", temp); // the length of the largest sub array whose elements are in increasing order
    sum(a,i,temp);      
    return 0;
}   

【问题讨论】:

  • 你遇到了什么问题
  • 我的程序输出不正确。看来我的逻辑是错误的什么的。

标签: c arrays file


【解决方案1】:

只是一个一般性建议,以防止您的程序崩溃:

改变这个:

while (!feof (f1) && fscanf (f1, "%d", &number) && i++ < 1000 )
    a[i] = number;

到这里:

while (!feof (f1) && fscanf (f1, "%d", &number) && i < 1000 )
    a[i++] = number;

【讨论】:

  • 已更改。顺便说一句,程序不会崩溃。
  • 小问题,我们可以对失败的 fopen 执行 feof,我认为 feof 检查分配给文件的结构中的标志,但如果文件从未打开,feof 将如何工作?哦,除非 feof(NULL) 返回一些东西。
  • 没有。你应该在调用f1 = fopen(...) 之后断言f1 != NULL
【解决方案2】:

好吧,我来自 C++,但也许我可以帮忙..你可以试试这个:

for(i=1;i<=1000;i++)
    {
     if(a[i]<a[i+1])
      counter++;

【讨论】:

  • Hmmmmm... 将访问a[1000]a[1001],其中仅允许a[0...999]。这不是一个好主意!!!
  • 我认为不会,因为当“i”变量为 1000 时,条件被评估为真,之后,它增加,现在 i=1001,逻辑条件评估为假因为 1001 不是
  • 哦..是的...对不起 :))
  • 首先,a[1000] 本身是不允许的。其次,您提到的任何事情都不会阻止CPU在i == 1000时访问a[i+1](因此访问a[1001])。
【解决方案3】:

我引入了一个新变量 temp_index,它将保存最大子数组的起始索引,并使用 j 来存储循环中的起始索引。

试试这个

 if(a[i-1]<a[i])
 { 
  if (counter == 0) { j = i }
  counter++;
 }
 else
 {
   if(counter>temp) {
     temp_index = j;
     temp=counter;
   }
   counter=0;
  } 
}
temp++;
printf("Temp= %d", temp); // the length of the largest sub array whose elements are in increasing order
sum(a,temp_index,temp)

【讨论】:

    【解决方案4】:

    a

    中定位最大的子数组

    “要找到最大的”,我想你的意思是最长的子数组。

    无论如何,这是一个实现(它适用于您的测试输入):

    int longest_increasing(int array[], int length, int *start, int *end)
    {
        int pos, curr_pos;
    
        *start = 1;
        *end = 0;
    
        curr_pos = 0;
        while (curr_pos + 1 < length) {
            while (curr_pos+1 < length && array[curr_pos] >= array[curr_pos+1])
                curr_pos++;
    
            for (pos = curr_pos; pos+1 < length && array[pos] < array[pos+1]; pos++)
                ;
    
            if (*end - *start < pos - curr_pos) {
                *start = curr_pos;
                *end = pos;
            }
            curr_pos = pos+1;
        }
    
        if (*start < *end)
            return 1;
        else
            return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 2019-10-16
      • 1970-01-01
      • 2020-09-22
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 2021-07-30
      相关资源
      最近更新 更多