CCF 201803-1 跳一跳 (C++)

201803-1 跳一跳

试题编号: 201803-1
试题名称: 跳一跳
时间限制: 1.0s
内存限制: 256.0MB
问题描述:CCF 201803-1 跳一跳 (C++)_第1张图片
解题过程

按题意解即可,这里代码仅用栈容器来记录是否为第一次跳跃,采用其他方法均可。

AC代码
#include
#include
using namespace std;
int main()
{
     
	int sum=0;
	stack<int> s;
	int n;
	int now=2;//表示每次应得分数 
	while(cin>>n&&n!=0)
	{
     
		if(n==1)
		{
     
			now=1;
			sum+=now;
			s.push(n);
		}
		if(n==2)
		{
     
			if(now==1||s.empty())
			{
     
				now=2;
				sum+=now;
				s.push(n);
			}
			else
			{
     
				now+=2;
				sum+=now;
				s.push(n); 
			}
		}
	}
	cout<<sum;
	return 0;
} 

你可能感兴趣的:(数据结构,算法,stack,栈,c++)