C 如何优化此代码以减少运行时间?
原来的问题是: 一个人有一辆相当旧的汽车,价值2000美元。 他看到一辆价值8000美元的二手车。他想保住他的老朋友 直到他能买到二手车 他认为他每个月可以节省1000美元,但他的旧车价格 而新一轮的降幅为每月1.5%。此外 在每个月底,损失百分比增加0.5% 两个月 你能帮助他吗?我们的人觉得很难做到这一切 计算 他要花多少个月才能攒够钱买房子 他想要一辆车,他还剩下多少钱 到目前为止,我已经实现了这一点,如何改进这段代码C 如何优化此代码以减少运行时间?,c,C,原来的问题是: 一个人有一辆相当旧的汽车,价值2000美元。 他看到一辆价值8000美元的二手车。他想保住他的老朋友 直到他能买到二手车 他认为他每个月可以节省1000美元,但他的旧车价格 而新一轮的降幅为每月1.5%。此外 在每个月底,损失百分比增加0.5% 两个月 你能帮助他吗?我们的人觉得很难做到这一切 计算 他要花多少个月才能攒够钱买房子 他想要一辆车,他还剩下多少钱 到目前为止,我已经实现了这一点,如何改进这段代码 int main (){ double percent=0.9
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);
}
已尝试解决您的问题并达成此解决方案:
#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;
}
#包括
int main(){
float priceOld=2000;//我想出售的商品的价格
float price new=8000;//我想买的商品的价格
int savingperMonth=1000;//我每月的储蓄
最后一个月的整数=0;
float finalLeft=0;
int mon=0;//月份数
for(mon=1;mon=priceNew){
最后几个月=周一;
finalLeft=(priceOld+(savingperMonth*mon))-priceNew;
printf(“月数:%d\n”,最后一个月);
printf(“最后剩余金额:%f$”,finalLeft);
打破
}
}
返回0;
}
我想这对你来说很好 主循环中的输出用于了解那里发生了什么:)
#包括
#包括
int main()
{
/*我可以用美分储存*/
无符号整数现在=0;
无符号整数工资=100000;
未签名的整数剩余;
未签字的旧车成本=200000;
未签字的新车成本=800000;
浮动减量百分比=0.015;
浮动下降百分比差异=0.005;
大小\u t个月=0;
而(1)
{
月份+++;/*月份已经过去*/
现钱+=薪水;/*拿到薪水*/
旧车成本-=旧车成本*减少百分比;/*旧车新成本*/
新车成本-=新车成本*减少百分比;/*新车新成本*/
/*
含义:
b-布格代特;
百分之p;
oc——旧车成本;
nc-新车成本。
*/
printf(“b:$%u;\tp:%.1f;\toc:$%u.%u;\tnc:$%u.%u\n”,
现款/100,折旧率*100,旧车成本/100,旧车成本%100,新车成本/100,新车成本%100);
/*如果(我们的钱+旧车的成本)够买新车*/
如果(新车成本你可以用数学来解决这个问题:
新车每月价格:
(新车成本)*(1-(0.015+0.005*楼层(月/2))^month
旧车每月价格:
(旧车成本)*(1-(0.015+0.005*楼层(月/2))^月
你的薪水:
1000*月
在第n个月,您将有:
新车成本=旧车成本+工资
把这个月加起来,你就解决了这个问题
你也可以简化这个等式。你的导师可能希望你用数学方法来解决这个问题,而不是用循环。也许你应该在循环内计算diff
,这样你就有机会结束你的程序了……通过“运行时间更短”你是说“避免无休止的循环”吗?你说“改进代码”到底是什么意思?它工作吗?它占用了大量的运行时间,所以我可以减少运行时间吗?非常感谢您的帮助,但我无法理解的是,我的算法与您的算法有何不同?
#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;
}