原文链接
原题链接
思路
当输入为0时,游戏结束
用pre变量保存上一跳的得分。容易知道,当本次为跳到中心,则直接加一分。如果跳到中心,则需要去考虑到前一次的得分。当pre为1时,那么该次的得分就是2;当pre不为1时,那么该次的得分就是pre+2.
代码
#include <bits/stdc++.h>
using namespace std;
long long ans=0,pre=1,tmp=0;
int main(){
while(true){
cin>>tmp;
if(tmp == 0){
// cout<<"结束!";
break;
}
if(tmp == 1){
ans += 1;//得一分
pre = 1;
}else if(tmp == 2){
if(pre == 1){ //前一局得分是一分
ans += 2;
pre = 2;
}else{
ans += pre + 2;
pre = pre + 2;
}
}
// cout<<"pre="<<pre<<" ans = "<<ans<<endl;
}
cout<<ans;
return 0;
}