具有未知变量列大小的输入数组

时间:2017-06-16 02:54:53

标签: c

我必须解决以下问题:

Raghav是一位在线购物者。他想订购一些产品。但在他订购产品之前,他想将其与其他商家进行比较。每行指定一个产品,但比较价格的限制仅限于最多三个商家。找到每行的最小值并添加价格和显示。

假设购买产品的限制为10。 假设每种产品的商家数量可能从1到3不等。

enter image description here

测试用例

输入1

Enter the number of products:
3
Enter the price per product:
110 102 100
250 200 312
412 450 475

输出1

The minimum cost of each product is as follows
100
200
412
Total amount you need to pay Rs.712

输入2

Enter the number of products:
13

输出2

You are allowed to buy maximum of 10 products

输入3

Enter the number of products:
3
Enter the price per product
100 250
500 300
450 350

输出3

The minimum cost of each product is as follows
100
300
350
Total amount you need to pay Rs.750

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#define NN 31

int main()
{
int n;
printf("Enter the number of products:\n");
scanf("%d",&n);

if (n>10)
{ 
 printf("You are allowed to buy maximum of 10 products"); return 0;
}

printf("Enter the price per product:\n");

//Storing  the input in a 2D string first

char **str=malloc((n)*sizeof(char *));
for(int i=0;i<n;i++)
{
    str[i]=malloc(NN);
}

scanf("\n");
for(int i=0;i<n;i++)
{
    fgets(str[i],NN,stdin);
}

int **arr=(int **)calloc(n,sizeof(int *));
for (int i=0;i<n;i++)
    arr[i]=(int *)calloc(3,sizeof(int));

//Conversion of 2D string to 2D integer array

int max;
for(int i=0;i<n;i++)
{
    int num, j = 0, len;
    max=j;
while ( sscanf( str[i], "%d%n", &num, &len) == 1 ) 
{
    arr[i][j]=num;
    str[i] += len;    
    j++;
    if(j>max) max=j;        //find the column size of 2D array   
}
}

//Calculation of minimum possible cost
int *min,amount=0;
 min=(int *)calloc(n,sizeof(min));

printf("The minimum cost of each product is as follows:\n");

for (int i=0;i<n;i++)
    {
    min[i]=arr[i][0];
    for(int j=1;j<max;j++)
    {
        if (arr[i][j]<min[i])
        {
            min[i]=arr[i][j];
        }
    }
    printf("%d\n",min[i]);
    amount=amount+min[i];
    }
 printf("Total amount you need to pay Rs.%d",amount);
 free(min);

for(int i=0;i<n;i++)
{
    free(str[i]);
    free(arr[i]);
}
free(str);
free(arr);
return 0;
}

如果输入如上所示提供,则此代码可以正常工作(我分别读取每一行并在遇到\ n时终止该行。)

但如果按以下方式提供成本输入,如何处理:

输入4

Enter the number of products:
3
Enter the price per product
100
250
500
300
450
350

同样,列大小可以在1到3之间变化,用户不指定列大小。

我需要有关如何在用户停止时停止数组输入的帮助。他/她可以为3种产品(n = 3)输入3或6或9个价格。

2 个答案:

答案 0 :(得分:1)

要停止用户输入,您可以读取用户输入的所有行(最后一种情况下最多9行),如果用户输入的行数超过n行,请检查后面的每一行,如果该行为空。如果line为空,则用户已完成输入。

答案 1 :(得分:0)

这是一个解决方案

添加循环以输入多个值, 当用户将产品数量设置为0时,中断循环。

免费修复str[i]

#include<stdio.h>
#include<stdlib.h>
#define NN 31

int main() 
{       
    int n;  
    while (42) {
        printf("Enter the number of products, 0 to quit:\n");
        scanf("%d",&n);

        if (n > 10)
        {
            printf("You are allowed to buy maximum of 10 products");
            continue;
        } else if (n == 0) {
            printf("Bye");
            return 0;
        }

        printf("Enter the price per product:\n");

        //Storing  the input in a 2D string first

        char **str=malloc((n)*sizeof(char *));
        for(int i=0;i<n;i++)
        {   
            str[i] = malloc(NN);
        }

        scanf("\n"); 
        for(int i=0;i<n;i++)
        {
            fgets(str[i], NN, stdin);
        }

        int **arr= (int **)calloc(n,sizeof(int *));
        for (int i = 0; i < n; i++)
            arr[i] = (int *)calloc(3,sizeof(int));

        //Conversion of 2D string to 2D integer array

        int max;
        for(int i=0;i<n;i++)
        {       
            int num, j = 0, len;
            char *p = str[i];
            max=j;
            while ( sscanf(p, "%d%n", &num, &len) == 1 )
            {
                arr[i][j]=num;
                p += len;
                j++;
                if(j>max) max=j;        //find the column size of 2D array
            }
        }

        //Calculation of minimum possible cost
        int *min,amount=0;
        min=(int *)calloc(n,sizeof(min));

        printf("The minimum cost of each product is as follows:\n");
        for (int i=0;i<n;i++)
        {       
            min[i]=arr[i][0];
            for(int j=1;j<max;j++)
            {
                if (arr[i][j]<min[i])
                {
                    min[i]=arr[i][j];
                }
            }
            printf("%d\n",min[i]);
            amount=amount+min[i];
        }
        printf("Total amount you need to pay Rs.%d\n",amount);
        free(min);

        for(int i=0;i<n;i++)
        {
            free(str[i]);
            free(arr[i]);
        }
        free(str);
        free(arr);
    }
    return 0;
}