codingfresher @ 2023-01-15 11:10:04
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int day=0;
int gold=0;//总金币数
int coin=1;//每天获得的金币
while(day!=n)
{
int k=1;
for(int i=0;i<k;i++)
{
gold=gold+coin;
cout << coin << "," << gold << endl;//用于观察coin和gold的变化情况
day++;
}
k++;
coin++;//每个周期后coin+1
}
cout << gold << endl;
return 0;
}
by SZ_Metro_Line14 @ 2023-01-15 11:52:07
@codingfresher 过样例了没(我没试你的代码)
by Typical_typhoon @ 2023-01-15 12:06:36
话不多说,先贴代码:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int day=0;
int gold=0;//总金币数
int coin=1;//每天获得的金币
int k=1;
while(day!=n)
{
bool f=1;
for(int i=0;i<k;i++)
{
gold=gold+coin;
day++;
if(day==n){
f=0;
break;
}
}
if(f==0)break;
k++;
coin++;//每个周期后coin+1
}
cout << gold << endl;
return 0;
}
为什么错? 因为你的k定义在了while里面,导致你的for循环只循环了1次,所以每次金币就会多加 另外,如果for循环中day不加判断也可能多加
by Typical_typhoon @ 2023-01-15 12:06:54
@codingfresher
by codingfresher @ 2023-01-16 10:43:23
@NB26 好的,知道了,谢谢