簡體   English   中英

我不明白為什么我的 for 循環給出了一個尷尬的 output

[英]I don't understand why my for loop gives an awkward output

不幸的是......我昨天問了關於另一個錯誤的確切問題,希望我不再有這些錯誤,但我仍然有這個尷尬的output,就像某種取決於數組元素的數字,如-4221565或-4647963,等等......直到現在我認為我的數組附加部分工作正常,我試過了。 但我猜想條件中的 -1 有問題,但我不能說出它的名字。 For循環也是我想說的另一個地方。 我從凌晨 3 點開始嘗試解決這個問題,然后拖延,然后嘗試...截止日期是 7.04.2020(今天)22.00,所以我開始發瘋了。 任何幫助將不勝感激。謝謝。所以這是問題(輸入不必為 15,最多可以為 15):

您必須將最多 15 種不同的貨物從一個港口運輸到另一個港口。 運輸這些貨物的貨船的承載能力為 50 噸。 枚舉負載,並給出有關每個負載重量的信息作為輸入。

假設每件貨物的重量小於等於 50 噸且大於 0。

您將在一行中從輸入中讀取每個負載的重量。 您的輸入將以 -1 結尾。 您將打印必要的旅行次數。

樣本輸入:

50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 -1

Output:15

輸入:20 25 25 36 37 25 20 10 50 9 16 45 32 10 25 -1

Output:11

輸入:14 16 50 10 10 19 45 40 32 24 25 47 10 12 25 -1

Output:9

輸入:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -1

Output:3

這是我的代碼:

#include <stdio.h>
int main()
{   int w,i,sum,index;
    int list[15];
    w = 1;
    index = 0;
    do
    {   scanf("%d",&w);
        list[index] = w;
        index++;
    }while(w != -1);
    sum = 0;
    for(i = 0;i < 15;i++)
    {
        sum +=list[i];
    }
        sum = sum / 50;
        printf("%d",sum);
    return 0;

}

在您的代碼中,您傳遞的數組邊界-1將是數組的第 16 個元素,這是錯誤的。 你至少需要int list[16]; .

但是您的解決方案是錯誤的,您正在根據給定的輸入和 output 判斷負載將它們放置在貨船上。 例如,如果一艘船上有 30 個和 10 個,您不能將 20 個負載分成兩個 10 個負載,以便在船上放置最多 50 噸的負載。

你需要的是:

  1. 考慮一艘0噸的船

  2. 在負載總和小於或等於50時向其添加負載

  3. 如果添加新負載,則當前船舶的總和高於50 counter_ship++ ,然后將該負載添加到新負載中。

int main(void) {
    int w;
    int sum = 0;
    int ship_counter = 0;
    scanf("%d", &w);

    while (w != -1) {
        if (sum + w > 50)
        {
            ship_counter++;
            sum = 0;
        }
        sum += w; 
        scanf("%d", &w);
    }
    if (sum != 50)
        ship_counter++;
    printf("%d", ship_counter);

    return 0;
}  

你的代碼有幾個問題,我感覺不太好改進其中仍然存在邏輯問題的地方,所以我做了一些我通常不會做的事情(因為顯然你似乎對此壓力很大,而且你很着急)和為您提供我自己的任務版本:

#include <stdio.h>

#define MAX_LOADS 15
#define MAX_CAP 50

int main()
{
    int w[MAX_LOADS+1];
    int trips = 0;
    int i = 0;
    int sum = 0;
    int max = 0;
    int loads = 0;
    int rest = 0;

    printf("Enter the Weights of the Loads:\n");    

    while(i < (MAX_LOADS + 1))
    {
        scanf(" %d",&w[i]);
        printf(" ");
        if(w[i] == -1)
        {
            break;
        }
        sum = sum + w[i];

        loads++;
        i++;
    }

    printf("\n");

    printf("%d Loads have a weight of %d tons.\n", loads, sum);

    if(sum <= MAX_CAP)
    {
         printf("The amount of needed trips are: 1");
    }
    else
    {
         for(int i = 0; i < loads; i++)
         {
             if(w[i] == MAX_CAP)
             {
                 w[i] = 0;
                 trips++;
                 continue; 
             }
             else if(w[i] < MAX_CAP && w[i] > 0)
             {
                 rest = MAX_CAP - w[i];
                 w[i] = 0;

                 for(int j = 0; j < loads; j++)
                 {
                     if(i == j)
                     continue;

                     if(w[j] == rest)
                     {
                        w[j] = 0;
                        break;
                     }
                     else if(w[j] < rest && w[j] > 0)
                     {
                        rest = rest - w[j];
                        w[j] = 0;
                     } 

                     if(rest == 0)
                     {
                        break;
                     }
                 } 

                 trips++;
             }
         }

         printf("The amount of needed trips are: %d", trips);
    }

    return 0;    
}

Output / 執行 1:

Enter the Weights of the Loads:    
10 20 40 -1                    

3 Loads have a weight of 70 tons.
The amount of needed trips are: 2

Output / 執行 2:

Enter the Weights of the Loads:          
50 50 50 50 50 -1   

5 Loads have a weight of 250 tons.              
The amount of needed trips are: 5

Output / 執行 3:

Enter the Weights of the Loads:  
10 10 10 10 10 -1   

5 Loads have a weight of 50 tons.                               
The amount of needed trips are: 1 

您需要對數組進行排序。 然后,您需要檢查閾值金額。 如果超過了,則添加一個容器。

#include <stdio.h>
void swap(int* xp, int* yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 

// Function to perform Selection Sort 
void selectionSort(int arr[], int n) 
{ 
    int i, j, min_idx; 

    // One by one move boundary of unsorted subarray 
    for (i = 0; i < n - 2; i++) { 

        // Find the minimum element in unsorted array 
        min_idx = i; 
        for (j = i + 1; j < n-1; j++) 
            if (arr[j] < arr[min_idx]) 
                min_idx = j; 

        // Swap the found minimum element 
        // with the first element 
        swap(&arr[min_idx], &arr[i]); 
    } 
} 

int main()
{   int w,i,sum,index;
    int list[15];
    w = 1;
    index = 0;
    do
    {   scanf("%d",&w);
        list[index] = w;
        index++;
    }while(w != -1);

    selectionSort(list,index);

    for (int i = 0; i < index; i++) 
        printf("%d ", list[i]); 
    printf("\n"); 

    int ans=0;
    int threshold=0;
    for(int i=0; i<index; ++i){
        if(threshold + list[i]<=50){
            threshold+=list[i];
        }
        else {
            threshold = list[i];
            ++ans;
        }
    }
    ans++;
    printf("%d", ans);
    return 0;

}

將 do-while 循環更改為 while,如下所示:

while(w != -1 && index < 15)
{   scanf("%d",&w);
    list[index] = w;
    index++;
}

這樣可以避免寫入超出數組的邊界。

那么,for循環的條件可能是:

for(i = 0;i < index; i++)

(索引的值來自上一個循環,它只到最后插入的下一項)。 所以你是在總結實際有輸入的項目,而不是整個數組。

當然,正如其他人所評論的那樣,您可以不使用數組。

希望有幫助

#include <stdio.h>
#define MAX_SIZE 15
int main()
{  
    /* Let's define variables here. */
    int i=0,j=0 ;
    int input;
    int weights[MAX_SIZE];
    int  numberOfTrips = 0;
    int sum = 0;

  /* Inputs is taken  here from usre, Max = 15 */
    printf("Input:\n");
    scanf("%d",&input);
    while(input != -1){
        if (i< MAX_SIZE){
            if (input <= 50 &&  input>= 0){
                weights[i] = input;
                i = i  +1;

            } else printf("Wrong Input! \nPlease Enter value between 0 and 50\n");
        }

        else printf("You reach MAX_SIZE, please Enter -1 to see the Output");


        scanf("%d",&input);

    }

    printf("\n");
   /* We are going  will count the number of trips necessary*/
    if(input == -1){
        if (weights[0] != 0){
             /* Let's test the first value*/
          numberOfTrips = numberOfTrips +1;
        }
        sum= weights[0];
        for (j = 1; j<i; ++j){
          sum = sum +weights[j];

          if (sum>50){
            numberOfTrips = numberOfTrips +1;
            sum = weights[j];
          }
        }

    }
 /*Finally, let's print the output here.*/
    printf("Output:\n");
    printf("%d",numberOfTrips);

    return 0;
}

暫無
暫無

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

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