वारहाउस केला प्रश्न उत्तर


गोदाम, जिसे दूसरे शब्दों में गोदाम कहा जाता है, एक ऐसी इमारत है जिसका उपयोग कच्चे माल या निर्मित वस्तुओं को तब तक संग्रहीत करने के लिए किया जाता है जब तक कि उन्हें अन्य स्थानों पर निर्यात नहीं किया जाता है।

ऐसे कई गोदाम हैं जिनका उपयोग बड़ी मात्रा में केले को संग्रहीत करने और पकाने के लिए किया जाता है। एक बार केले पक जाने के बाद, प्रत्येक गोदाम मालिक उन सभी केलों को एक इकाई के रूप में पैक करेगा और उन्हें अन्य देशों में निर्यात करने के लिए हवाई अड्डे पर ले जाएगा। ये सभी गोदाम एक-दूसरे के करीब हैं और मालिक मिलनसार हैं। इसलिए केले का परिवहन करते समय, वे परिवहन लागत को कम करने के लिए वाहन (यदि संभव हो) साझा करना चाहेंगे। लेकिन केवल दो लोग ही एक वाहन साझा कर सकते हैं और प्रत्येक वाहन एक समय में “डब्ल्यू” टन का वजन ले जा सकता है।

प्रत्येक मालिक के केले के वजन का प्रतिनिधित्व करने वाली एक श्रृंखला को देखते हुए, वाहन द्वारा धारण की जाने वाली अधिकतम सीमा, सभी केलों को हवाई अड्डे तक ले जाने के लिए आवश्यक न्यूनतम संख्या में वाहनों का पता लगाएं।
ध्यान दें: ऐसा कोई भार नहीं है जो दी गई सीमा से अधिक भारी हो।
केले का वजन टन में मापा जाता है।

मैंने क्या प्रयास किया है:

कोड का गलत उत्तर मिल रहा है

सी
#include <stdio.h>
#include <stdlib.h>

void clearInputBuffer() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
}

int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

int minVehicles(int arr[], int n, int capacity) {
    // Sort the array in non-decreasing order
    qsort(arr, n, sizeof(int), compare);

    // Use two pointers approach to count the number of vehicles needed
    int count = 0;
    int i = 0;
    int j = n - 1;
    while (i <= j) {
        // If the sum of the current pair is less than or equal to the capacity, load both items
        if (arr[i] + arr[j] <= capacity) {
            i++;
            j--;
        } else {
            // If not, load the heavier item in a separate vehicle
            j--;
        }
        count++;
    }

    return count;
}

int main() {
    int n, capacity;
    printf("Enter the weights of the items separated by space: ");
    
    // Read the weights of the items in a single line
    scanf("%d", &n);
    int *arr = (int *)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    clearInputBuffer(); // Clear the input buffer

    printf("Enter the capacity of each vehicle: ");
    scanf("%d", &capacity);

    int result = minVehicles(arr, n, capacity);

    printf("Minimum number of vehicles required: %d\n", result);

    free(arr);

    return 0;
}

समाधान 2

आपको अपना कोड साफ और डीबग करना होगा। बेहतर उपयोगकर्ता सहभागिता के साथ शुरुआत करें।

सी++
int main() {
    int n, capacity;
    printf("Enter the count of the items: ");
    
    // Read the weights of the items in a single line
    scanf("%d", &n);
    int *arr = (int *)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++) {
        printf("Enter the weights of the item %d separated by space: ", i);
        scanf("%d", &arr[i]);
    }

बेहतर पठनीयता के लिए आप अपने कोड में कुछ संरचना का बेहतर उपयोग कर सकते हैं, जैसे किसी वाहन के लिए

सी++
struct Vehicle {
 int identifier; // like index
 int maxLoad;
 int currentLoad;
}

तुलना की तरह, अधिक आउटपुट बनाएं।

कुछ पर जाएँ सी ट्यूटोरियल सीखें समाधान करना तुम्हारा गृहकार्य.

コメント

タイトルとURLをコピーしました