【问题标题】:Write a program that sums the sequence of integers, as well as the smallest in the sequence编写一个程序,对整数序列以及序列中的最小值求和
【发布时间】:2010-10-05 01:25:22
【问题描述】:

编写一个对序列求和的程序 整数以及最小的 序列。假设第一个 使用 scanf 读取的整数指定 剩余的值的数量 进入。例如序列 输入:

输入:5 100 350 400 550 678

输出:序列的总和 整数是:2078

输入:5 40 67 9 13 98

输出:最小的整数 输入的是:9

这是我每天都在研究的问题,但是通过查看这个问题,5 不是最小的整数吗?我不知道如何编写这个程序。感谢任何帮助

【问题讨论】:

  • 这是 C 编程语言吗?
  • 问题是“用 scanf 阅读”。可以放心地假设它是 C。
  • 我认为这个问题不应该被否决......他不是要我们为他写这个问题,他只是无法理解这个问题。
  • 5 是序列的大小/长度。否则,你怎么知道最后一个数字是多少?
  • 对于这样一个措辞不当的问题,我深表歉意。在阅读和了解该网站的工作原理之前,我只是将其剪切并粘贴到框中。我很感激那些愿意帮助的人,也很抱歉那些让我心烦意乱的人。不幸的是,这是向我们新手教授的内容的一个示例。 :-(

标签: c


【解决方案1】:

首先,5 不被视为列表的一部分,它是列表的计数。因此,它不应包含在计算中。

因为这是作业,所以这里是伪代码。你的工作是首先理解伪代码(使用示例输入在你的脑海中运行它)然后将其转换为 C 代码并尝试让它编译并成功运行(使用相同的示例输入)。

我建议将样本输入“2 7 3”(两个项目,分别是 7 和 3)作为一个好的起点,因为它很小,总和为 10,最小的 3。

如果您尝试这样做超过一天,请将您的代码发布到此问题中作为编辑,我们会看看我们能做些什么来帮助您。

get a number into quantity
set sum to zero
loop varying index from 1 to quantity
    get a number into value
    add value to sum
    if index is 1
        set smallest to value
    else
        if value is less than smallest
            set smallest to value
        endif
    endif
endloop
output "The sum of the sequence of integers is: ", sum
output "The smallest of the integers entered is: ", smallest

Stack Overflow 似乎分为三个阵营,那些只会给你代码的阵营,那些会告诉你推迟并完成自己的作业的阵营,以及像我这样更愿意看到你接受教育的阵营 - 由当你进入劳动力市场时,我希望退休,这样你就不会与 me 竞争 :-)。

在任何人发现我的算法中的漏洞之前,这是为了教育。我至少留下了一个陷阱来帮助训练这个家伙 - 可能还有其他人,我声称我故意把它们放在那里是为了测试他:-)。


更新:

罗伯特,在我已经评论过你的(非常好的)尝试之后,这就是我修改你的代码来完成任务的方式(当然是你的,而不是我的)。您有望看到我的 cmets 如何修改代码以达到此解决方案:

#include <stdio.h>
int main (int argCount, char *argVal[]) {
    int i;              // General purpose counter.
    int smallNum;       // Holds the smallest number.
    int numSum;         // Holds the sum of all numbers.
    int currentNum;     // Holds the current number.
    int numCount;       // Holds the count of numbers.

    // Get count of numbers and make sure it's in range 1 through 50.

    printf ("How many numbers will be entered (max 50)? ");
    scanf ("%d", &numCount);
    if ((numCount < 1) || (numCount > 50)) {
        printf ("Invalid count of %d.\n", numCount);
        return 1;
    }
    printf("\nEnter %d numbers then press enter after each entry:\n",
        numCount);

    // Set initial sum to zero, numbers will be added to this.

    numSum = 0;

    // Loop, getting and processing all numbers.

    for (i = 0; i < numCount; i++) {

        // Get the number.

        printf("%2d> ", i+1);
        scanf("%d", &currentNum);

        // Add the number to sum.

        numSum += currentNum;

        // First number entered is always lowest.

        if (i == 0) {
            smallNum = currentNum;
        } else {
            // Replace if current is smaller.

            if (currentNum < smallNum) {
                smallNum = currentNum;
            }
        }
    }

    // Output results.

    printf ("The sum of the numbers is: %d\n", numSum);
    printf ("The smallest number is:    %d\n", smallNum);

    return 0;
}

这是您的示例数据的输出:

pax> ./qq
How many numbers will be entered (max 50)? 5

Enter 5 numbers then press enter after each entry:
 1> 100
 2> 350
 3> 400
 4> 550
 5> 678
The sum of the numbers is: 2078
The smallest number is:    100

pax> ./qq
How many numbers will be entered (max 50)? 5

Enter 5 numbers then press enter after each entry:
 1> 40
 2> 67
 3> 9
 4> 13
 5> 98
The sum of the numbers is: 227
The smallest number is:    9

pax> ./qq
How many numbers will be entered (max 50)? 0
Invalid count of 0.

[fury]$ ./qq
How many numbers will be entered (max 50)? 51
Invalid count of 51.

顺便说一句,请确保您始终将 cmets 添加到您的代码中。教育工作者喜欢这种东西。必须在 10 年后尝试理解您的代码的开发人员也是如此。

【讨论】:

  • 感谢 Pax,这里有很多值得细细琢磨的。为你的方法干杯,我希望我的导师对知识有同样的重视。不用担心我与任何人竞争,我是一名在学校试图获得学位的图形多媒体专家。信不信由你,我当然想教 H.S.Not 编程。
  • 比 +1 更好!!!!,我发布了我想出的代码。我想我可能也学到了一些东西。(darn...j/k) 无论如何,我真的很感谢帮助和指导,我保证会提出正确的问题并做我自己的工作。
【解决方案2】:

阅读:

假设读取的第一个整数 用 scanf 指定数量 剩余值待输入

所以它不是序列的一部分......

剩下的就是你的作业(和 C...)

【讨论】:

    【解决方案3】:

    没有。 5 是您必须读入列表的整数个数。

    【讨论】:

      【解决方案4】:

      Jeebus,我不是在为你做作业,但是......

      你有没有停下来在纸上草草写下它并弄清楚它应该如何工作?编写一些伪代码,然后转录为真实代码。我本来以为:

      • 读取整数
      • 循环多次 ** 阅读更多整数 ** 添加 ** 找到最小的

      如果您使用 C 语言,请查看 INT_MAX - 这将有助于找到最小的整数。

      【讨论】:

        【解决方案5】:

        由于整数列表是可变的,我很想使用 strtok 将字符串拆分为单独的字符串(以空格分隔),然后使用 atoi 转换每个数字并即时求和或找到最小值。

        -亚当

        【讨论】:

        • 我认为整数不会出现在单个字符串中。我认为它们将一次被发送到输入,因此每个 scanf 调用都将返回一个整数。
        • scanf("%d") 在一行上处理多个值就好了,IIRC(我可能记错了 cin :-)。
        • @Pax:是的。对于scanf,空白就是空白。不管是空格还是换行符。
        【解决方案6】:

        首先您读取值的数量(即 5),然后创建一个由 5 个元素组成的 int 数组,读取其余的输入,拆分它们并将它们放入数组中(在将它们转换为整数之后)。

        然后在数组上做一个循环,得到的总和找到最小值。

        希望有帮助

        【讨论】:

        • 只是出于兴趣,为什么是数组?您可以一次处理一个数字,然后将最小的数字相加。
        【解决方案7】:

        不是[']找你们来做这项工作

        酷。当您向他们倾倒问题文本并且问题文本以命令式形式(“做这个!写那个!等等”)时,人们往往会生气。

        您可能想说“我遇到了一个家庭作业问题。问题是:写一个 [...]。我不明白为什么 [...]。”

        【讨论】:

          【解决方案8】:
          #include <stdio.h>
          
          main ()
          {
              int num1, num2, num3, num4, num5, num6, i;
              int smallestnumber=0;
              int sum=0;
              int numbers[50];
              int count;
              num1 = 0;
              num2 = 0;
              num3 = 0;
              num4 = 0;
              num5 = 0;
              num6 = 0;
          
              printf("How many numbers will be entered (max 50)? ");
              scanf("%d", &count);
          
              printf("\nEnter %d numbers then press enter after each entry:  \n", count);
          
              for (i=0; i < count; i++) {
                  printf("%2d> ", i+1);
                  scanf("%d", &numbers[i]);
                  sum +=  numbers[i];
              }
          
              smallestnumber = numbers[0];
              for (i=0; i < count; i++) {
                  if ( numbers[i] < smallestnumber)
                  {
                      smallestnumber = numbers[i];
                  }
              }
          
              printf("the sum of the numbers is: %d\n", sum);
              printf("The smallest number is: %d", smallestnumber);
          }
          

          【讨论】:

          • PAX,你觉得编程怎么样?谢谢您的帮助。我认为数字无关紧要,它是供用户选择的吗?你发帖后灯泡熄灭了。很高兴得到回复并感谢您的帮助!!!!
          • 您可以编辑您的帖子并在代码行前放置 4 个空格,它将为您格式化。
          • 辛苦了!点,一次几个。 (1) num1 到 num6 不用于任何用途,我怀疑它们是您早期版本的遗留物。摆脱他们。 (2) 在使用之前,您可能需要确保将 count 设置为 1 到 50(打印错误并退出)...
          • (3) 您不需要数组,但这是一个实现决定。我不会,但这样做并没有错,特别是因为你的数量有限。
          • (4) 你的最后一个循环可以从 1 而不是 0 开始,因为你已经加载了 numbers[0]。
          猜你喜欢
          • 2021-09-05
          • 2012-10-02
          • 1970-01-01
          • 2014-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多