Dream_liu @ 2023-09-05 18:33:03
#include<iostream>
using namespace std;
int main()
{
int day, coin = 1,sum = 0;
cin >> day;
for (int i = 0;i < day;i++)
{
for (int k = 0; k < coin; k++)
{
sum += coin;
}
coin++;
if (coin == day)
{
break;
}
}
cout << sum << endl;
system("pause");
return 0;
}
我的想法是两个循环,内循环进行累加,外循环进行金币的递增,为啥这样结果有点问题呢?
by zacharyzhong @ 2023-09-05 18:41:24
一堆问题:
if (coin == day)
{
break;
}
啥意思,为什么要退出;
for (int k = 0; k < coin; k++)
{
sum += coin;
}
coin++;
累加也不是这么累加啊
by Dream_liu @ 2023-09-05 19:08:40
@Zacharyzhong 大佬可以给点改进方向吗?
by zhangxuhao2010 @ 2023-09-05 19:32:23
#include<iostream>
using namespace std;
int main()
{
int day, coin = 1,sum = 0,i=0;
cin >> day;
for (;;)
{
for (int k = 0; k < coin; k++)
{
sum += coin;
i++;
if (i == day)
{
break;
}
}
if(i==day){
break;
}
coin++;
}
cout << sum << endl;
return 0;
}
by Dream_liu @ 2023-09-05 19:44:27
@zhangxuhao2010 感谢感谢