簡體   English   中英

編寫一個程序,該程序求和整數序列以及該序列中的最小整數

[英]Write a program that sums the sequence of integers, as well as the smallest in the sequence

編寫一個程序,該程序求和整數序列以及該序列中的最小整數。 假定用scanf讀取的第一個整數指定要輸入的剩余值數。 例如,輸入的序列:

輸入:5100350400550678

輸出:整數序列的總和是:2078

輸入:5 40 67 9 13 98

輸出:輸入的最小整數為:9

這是我每天都在處理的問題,但通過查看,這不是最小的整數5嗎? 我不知道如何編寫該程序。 感謝任何幫助

第一件事,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似乎分為三個陣營,那些陣營只為您提供代碼,那些陣營告訴您推遲並完成自己的作業,以及那些像我一樣希望在您擊中時接受過教育的陣營。勞動力,我希望退休,這樣您就不會和競爭:-)。

而且,在有人對我的算法有所了解之前,這是為了教育 我至少留了一個陷阱來幫助訓練這個人-可能還有其他人,我聲稱我故意把他們放在那里測試他:-)。


更新:

羅伯特,經過您的(非常好)嘗試(我已經評論過),這就是我修改您的代碼來完成任務的方式(當然,請交給您,而不是我的)。 您可以希望看到我的注釋如何修改代碼以實現此解決方案:

#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.

順便說一句,請確保始終在代碼中添加注釋。 教育工作者喜歡這種東西。 因此,必須在十年后嘗試理解您的代碼的開發人員也是如此。

讀:

假設用scanf讀取的第一個整數指定要輸入的剩余值數

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

剩下的就是你的功課(和C ...)

No. 5是必須讀入列表的整數數量。

Jeebus,我不是在為您做作業,但是...

您是否已停止將其從紙上划掉,並確定其工作方式? 編寫一些偽代碼,然后轉錄為真實代碼。 我曾想過:

  • 讀取整數
  • 循環多次**閱讀更多整數**添加**查找最小

如果您使用C語言,請查看INT_MAX-這將有助於找出最小的整數。

由於整數列表是可變的,因此我很想使用strtok將字符串拆分成單個字符串(按空格分隔),然后使用atoi轉換每個數字並即時求和或求最小值。

-亞當

首先,您讀取值的數量(即5),然后創建一個由5個元素組成的int數組,讀取其余輸入,將它們拆分並將它們放入數組中(將它們轉換為整數之后)。

然后在數組上循環以求和以找到最小值。

希望能有所幫助

不是在找你們來做這項工作

涼。 當您將問題文本轉儲給他們時,人們往往會發脾氣,而問題文本則以命令式的措詞表達(“這樣做!寫那個!等”)。

您可能想說些類似的話:“我遇到家庭作業問題。這就是問題:寫[...]。我不明白為什么[...]。”

#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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM