从数字中找出3个连续数字的最大总和

时间:2013-12-02 17:56:54

标签: c encoding

我有一个任务是编写C程序,找到数字中连续3位数的最大总和。我写了它,但我遇到了麻烦:程序似乎运行正常,但它没有正确的答案,而是输入一些奇怪的数字。 例如,如果输入为“560315”,则响应为“155”。但是,如果我键入“560415”,答案仍为“155”,如果输入“561315”,则响应变为“156”。这是编码的问题吗? 这是代码:

#include <stdio.h>
int isempt(int a) {
if ((a==' ')||(a=='\n')||(a=='\t')) {
    return 1;
}
else return 0;
}

main() {
int a, b, c, d, e, i, maxsum;
a = 0;
b = 0;
c = 0;
d = 0;
maxsum = 0;
i = 0;
int counter = 0; //the variables "numsearch" and "inside" indicate whether program is in the number or outside it
int numsearch = 1; 
int inside = 0;
while ((i = getchar())!=EOF) {
    if (numsearch==1) {
        if (isempt(i)==0) {
            numsearch = 0;
            inside = 1;
            a = i;
            counter++;
        }
        }
    else if ((isempt(i)==0)&&(inside==1)) {
            if (counter == 1) {
                b = i;
                counter++;
            }
            else if (counter == 2) {
                c = i;
                counter++;
            }
            else if (counter == 3) {
                d = i;
                maxsum = a+b+c;
                if ((b+c+d) > maxsum) {
                    maxsum =( b+c+d);
                }
                a =b;
                b = c;
                c = d;
                counter++;
            }
            else if (counter == 4) {
                d = i;
                if ((b+c+d)>maxsum) {
                    maxsum = b+c+d;
                }
                a=b;
                b=c;
                c=d;
            }

            }
        else if ((counter>=3)&&(isempt(i)==1)) {
                printf("\n%d\n", maxsum );
                counter = 0;
                numsearch = 1;
                inside = 0;
                a = 0;
                b = 0;
                c = 0;
                d = 0;
            }
            else {
                counter = 0;
                numsearch = 1;
                inside = 0;
                a = 0;
                b = 0;
                c = 0;
                d = 0;

            }
        }
    }

4 个答案:

答案 0 :(得分:4)

要解决此问题,每次使用变量48的值时,您都可以从i中减去i,以将其分配给算法的变量:abcd,在这种情况下,您的示例560316的答案将是11,我相信它是3个连续数字的最大总和,这将起作用,因为{{1数字0的ascii代码,49代表数字1,依此类推,所以你的代码应该是这样的:

48

答案 1 :(得分:2)

你有很多嵌套条件,我认为你可能会让自己感到困惑。我相信制作一个整数数组会更容易,并且在数组的每个索引处初始化为零。然后将索引指示的数字加一(第一个数字到第0个索引,第二个数字到第一个索引等)添加到该索引和前两个索引(如果它们存在,则必须检查边界)。然后找到并打印数组中最大的数字。

答案 2 :(得分:1)

@ T-D的主要问题很好:使用a = i - '0'而不是a = i。 (在4个地方)
@Jonah Nelson确实指出嵌套有点过分。随后是简化版本 OP代码确实有文本既不是空白也不是数字。

#include <stdio.h>
#include <ctype.h>

int main() {
  int ch;
  int index = 0;
  char history[2];
  int maxsum = -1;
  int sum = 0;  // running sum
  while ((ch = getchar()) != EOF) {
    if (isdigit(ch)) {
      ch -= '0';  // Convert text code to `int` value.
      sum += ch;
      if (index >= 2) {
        if (sum > maxsum) {
          maxsum = sum;
        }
        sum -= history[0];
        history[0] = history[1];
        history[1] = ch;
      }
      else {
        history[index++] = ch;
      }
    }
    else {
      if (!isspace(ch)) {
        printf("Unexpected text '%c'\n", ch);
      }
      index = 0;
      sum = 0;
    }
  }
  printf("Maximum sum: %d\n", maxsum);
  return maxsum < 0;
}

答案 3 :(得分:0)

如果你想要一个非常简单的程序,你不需要索引到一个数组。记住最后2位数字。因此:

#include <stdio.h>
#include <ctype.h>

int main() 
{
    int ch;
    int count = 0;
    int first = 0;
    int second = 0;
    int sum = 0;  /* running sum */
    int max_sum = 0; /* max sum */
    while (ch = getchar(), ch != EOF) 
    {
        if (isdigit(ch))
        {
            ++count;
            ch -= '0';  /* Convert text code to `int` value. */
            sum += ch;
            if (sum > max_sum) 
            {
                max_sum = sum;
            }
            sum -= first;
            first = second;
            second = ch;
        }
        else
        {
            printf("Ignored unexpected text '%c'\n", ch);
            /* Optionally reset first, second, sum and count to 0 */
        }
    }
    printf("Maximum sum: %d\n", max_sum);
    return count < 3; /* Didn't get enough characters */
}