C语言输入未知数目的若干个整数

因为输入个数未知,因此应该先准备一个足够大的整型数组;

然后利用循环进行输入,判断条件即为是否输入换行符。

PAT题集《浙大版题目集》练习3-8 查询水果价格 (15 分)答案:

#include



int main()

{

    int choices[20];

    int i = 0;

    do

    {

        scanf("%d", &choices[i]);

        i++;

    } while (getchar() != '\n');

    printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit\n");

    for (int j = 0; j<5; j++)

    {

        if (choices[j] == 0)

            break;

        else

        {

            switch (choices[j])

            {

            case 1:printf("price = 3.00\n"); break;

            case 2:printf("price = 2.50\n"); break;

            case 3:printf("price = 4.10\n"); break;

            case 4:printf("price = 10.20\n"); break;

            default:printf("price = 0.00\n");break;

            }



        }   

    }



    return 0;

}

你可能感兴趣的:(pat练习题,C语言输入)