Expression Result Unused Greedy Algorithm

后端 未结 3 1945
南方客
南方客 2021-01-28 08:18

I have run this program and get the error expression result unused. I may be doing something simple wrong, but I have spent the day trying to figure it out to no avail. Any help

3条回答
  •  一向
    一向 (楼主)
    2021-01-28 08:21

    All the 4 statements x - 25, x- 10, x- 5, x - 1 will turn out to be useless unless you assign that value to x; Because you are trying to subtract the value from x, but you are not assigning the new value to x.

    Here is the solution of your problem:

    #include 
    #include 
    
    int main()
    {
        int x, y = 0;
        printf("Enter the amount of change ");
        x = GetFloat() * 100;
        while (x != 0)
        {
            if (x >= 25)
            {
                x = x - 25;   //or x-=25;
                y = y + 1;
            }
    
    
            if (x >= 10 && x < 25)
            {
                x = x - 10;   //or x-=10;
                y = y + 1;
            }
            if (x >= 5 && x < 10)
            {
                x = x - 5;    //or x-=5;
                y = y + 1;
            }
            if (x >= 1 && x < 5)
            {
                x = x - 1;     //or x-=1; or x--; or --x; :)
                y = y + 1;
            }
       }
       printf("The number of coins neccessary is %d", y);
    }
    

提交回复
热议问题