15 条回复
我的为什么一直90%,楼主能帮忙看看可以吗?
#include <iostream> #include <istream> using namespace std; long long computeShares(int day) { long long share(0); if(day==1) share=1; else { int dayCount(0),minusPeriod(1); bool minus(false); int periodCount(0); while(dayCount<day) { if(minus) { share--; periodCount=0; minusPeriod++; minus=false; } else { share++; periodCount++; if(periodCount==minusPeriod) minus=true; } dayCount++; } } return share; } int main() { int day(0); while(cin>>day) cout<<computeShares(day)<<endl; return 0; }
额,先说下我的思路吧,纯数学题的感觉。
题目中前几天的股价变化是1 2 | 1 2 3| 2 3 4 5| 4 5 6 7 8|.......,分别是2,3,4,5项,并且每项之间都是递增的,也就是说我们需要找到所求天数所在的区间,求出下限的股价值,再用输入的天数减去下限,便是下限的股价值应该加上的值,即所求的股价值。
额,好像没说清。。。。。
选取每一项的第一项,即1,1,2,4,7......,规律出来了,每项之间相隔一个 0,1,2,3.......这样的一个等差数列。
于是。。。。
n 1 2 3 4 5
Bn 1 3 6 10 15 (实际天数)
Cn 1 1 2 4 7 (股价)
Bn与n间存在明显的关系,同样,Cn与n也存在关系。于是:
Cn=(n*n-3*n+4)/2;Bn=(n*n+n)/2;
设输入天数为a,则n*n+n=2*a;对所求的n下取整即为所求下限Bn所代表的n值,则Bn与Cn可求,即得出第a天的股价
P.S. 楼主在本周日有在线笔试,所以抱歉暂时不能帮你们看了,不过你们也可以把你们代码的思路发出来0.0;相互借鉴学习啊
数据是没问题的。。。。。
上面的coder_2FEZDW7V说的没错。。。犯了低级错误,重新修改了一下,可以AC了!感谢各位大神!
#include <iostream> #include <istream> using namespace std; long long computeShares(int day) { long long share(1); //long long share(0); if(day==1) share=1; else { int dayCount(1)/*已过天数int dayCount(0)*/,minusPeriod(1); bool minus(false);//股票下跌标记 int periodCount(0); while(dayCount<day) { if(minus) { share--; periodCount=0; minusPeriod++; minus=false; } else { share++; periodCount++; if(periodCount==minusPeriod) minus=true; } dayCount++; } } return share; } int main() { int day(0); while(cin>>day) cout<<computeShares(day)<<endl; return 0; }
添加回复