how can i optimise this code so that it takes lesser run time?

时间:2018-05-28 18:47:36

标签: c

The original question is:

A man has a rather old car being worth $2000. He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one.

He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore the percent of loss increases by a fixed 0.5 percent at the end of every two months.

Can you help him? Our man finds it difficult to make all these calculations.

How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?

I have implemented this so far how can I improve this code.

int main (){
    double percent=0.985; // the value by which the price of an item //reduces
    int startPriceOld=2000; //price of an item i want to sell
    int startPriceNew=8000;// price of an item i want to buy
    int savingperMonth=1000; // my monthly savings  
    int init=0;
    int mon=0; //no of months
    int saving=0;
    int diff=(saving*mon)-((pow(percent,mon))*(startPriceNew-startPriceOld));
      /*checking the condition when the money I have by selling my old item in addition to my savings will be greater than the money I need to buy my new item*/
      while(1)
      {
          mon++;
          startPriceOld=percent*startPriceOld;
          startPriceNew=percent*startPriceNew;
          saving=init+mon*savingperMonth;
          if(diff>0)
              {break;}
      }
          int arr[2];
          arr[0]=mon;
          arr[1]=diff;
          printf("%d",mon);
}

3 个答案:

答案 0 :(得分:1)

你可以用数学来解决这个问题:

每个月新车的价格:
Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt-get -f install' to correct these. The following packages have unmet dependencies: mariadb-client : Depends: mariadb-client-10.0 (>= 10.0.34-0ubuntu0.16.04.1) but it is not installed mariadb-server-10.0 : PreDepends: mariadb-common (>= 10.0.34-0ubuntu0.16.04.1) but it is not installed Depends: mariadb-client-10.0 (>= 10.0.34-0ubuntu0.16.04.1) but it is not installed Breaks: mysql-server mariadb-server-core-10.0 : Depends: mariadb-common (>= 10.0.34-0ubuntu0.16.04.1) but it is not installed mysql-server : Depends: mysql-community-server (= 5.7.22-1ubuntu16.04) but it is not installed E: Unmet dependencies. Try using -f.

每个月旧车的价格:
(new_car_cost)*(1-(0.015+0.005*floor(month/2)))^month

你的薪水:
(old_car_cost)*(1-(0.015+0.005*floor(month/2))^month

在第n个月,您将拥有:
1000*month

整月,你已经解决了问题

您也可以简化等式。

答案 1 :(得分:0)

尝试解决您的问题并达成此解决方案:

#include <stdio.h>

    int main (){
        float priceOld=2000; //price of an item i want to sell
        float priceNew=8000;// price of an item i want to buy
        int savingperMonth=1000; // my monthly savings
        int finalMonths = 0;
        float finalLeft = 0;
        int mon=0; //no of months
        for (mon = 1; mon<9;mon++){
            priceOld = priceOld - ((priceOld*15)/100);
            priceNew = priceNew - ((priceNew*15)/100);
            if( mon % 2 == 0 ){
                priceOld = priceOld - ((priceOld*5)/100);
                priceNew = priceNew - ((priceNew*5)/100);
            }
            if((priceOld + (savingperMonth*mon)) >= priceNew){
                finalMonths = mon;
                finalLeft = (priceOld + (savingperMonth*mon)) - priceNew;
                printf("Number of months: %d\n", finalMonths);
                printf("Final amount left: %f$", finalLeft);
                break;
            }
        }
    return 0;
    }

我认为这对你很好。!

答案 2 :(得分:0)

主循环中的输出是为了解那里发生的事情:)

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

int main ( )
{
    /* will store it in cents */

    unsigned int money_now = 0; 
    unsigned int salary = 100000;
    unsigned int money_left;

    unsigned int old_car_cost = 200000;
    unsigned int new_car_cost = 800000;

    float decr_percent = 0.015;
    float decr_percent_diff = 0.005;

    size_t months = 0;

    while ( 1 )
    {
        months++; /* Month has passed */

        money_now += salary; /* got salary*/
        old_car_cost -= old_car_cost * decr_percent; /* new cost of old car */
        new_car_cost -= new_car_cost * decr_percent; /* new cost of new car */

        /* 
            meanings:
            b - bugdet;
            p - percent;
            oc - old car cost;
            nc - new car cost.
        */
        printf("b:$%u;\tp:%.1f;\toc:$%u.%u;\tnc:$%u.%u\n", 
            money_now / 100, decr_percent * 100, old_car_cost / 100, old_car_cost % 100, new_car_cost / 100, new_car_cost % 100 );

        /* if ( ours money + old car's cost ) is enough for new car  */
        if ( new_car_cost <= ( money_now + old_car_cost) ) break;

        /*if it's end of every second month */
        if ( 0 == (months % 2) ) decr_percent += decr_percent_diff;
    }

    puts(""); /* newline */
    printf("It has been %lu months.\n", months );
    money_left = ( money_now + old_car_cost ) - new_car_cost;
    printf("Money left : $%u.%u\n", money_left / 100, money_left % 100);

    return 0;
}