数学题之 P12206 [蓝桥杯 2023 国 Python B] 弹珠堆放 - 题解
longyitongxue · · 题解
Click this to the question.
做法
我们先找找规律。观察图片,我们发现:
- 第一层有
1 颗珠子。 - 第二层有
1+2 颗珠子。 - 第三层有
1+2+3 颗珠子。 - 第四层有
1+2+3+4 颗珠子。 - 第五层有
1+2+3+4+5 颗珠子。
到这里,我们发现,第 for(int i=1;;i++)(Python 可以使用一个循环变量 while True)的无限循环,并用 long long(Python 选手不用考虑)。
[^1]: 这里说明一下为什么
AC code
C++
#include<iostream>
using namespace std;
int main(){
long long cnt=0,gs=0;
for(int i=1;;i++){
gs+=i;
cnt+=gs;
if(cnt>20230610){
cout<<i-1;
break;
}
}
return 0;
}
Python
i=1
cnt=0
gs=0
while True:
gs+=i
cnt+=gs
if cnt>20230610:
print(i-1)
break
i+=1