问题描述
试题编号: | 201803-1 |
试题名称: | 跳一跳 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱。 输入格式 输入包含多个数字,用空格分隔,每个数字都是1,2,0之一,1表示此次跳跃跳到了方块上但是没有跳到中心,2表示此次跳跃跳到了方块上并且跳到了方块中心,0表示此次跳跃没有跳到方块上(此时游戏结束)。 输出格式 输出一个整数,为本局游戏的得分(在本题的规则下)。 样例输入 1 1 2 2 2 1 1 2 2 0 样例输出 22 数据规模和约定 对于所有评测用例,输入的数字不超过30个,保证0正好出现一次且为最后一个数字。 |
解题思路:
就按照规则一步步来就可以了。
具体代码:
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int a[40];
int score,last;
score=last=0;
cin>>a[0];
if(a[0]==1){ //先把特别的列出来
score=score+1;
}else if(a[0]==2){
score=score+2;
last=last+2;
}
for(int i=1;;i++){
cin>>a[i];
if(a[i]==0){
break;
}
if(a[i]==1){
score++;
last=0;//证明这一局没有到中心
}else if(a[i]==2){
if(last>0){
last=last+2;
score=last+score;
}else{
score=score+2;
last=2;
}
}
// cout<<score<<endl;
}
cout<<score<<endl;
return 0;
}
其实后面发现直接用分数做判断就可以了,每个数组直接改分数,然后根据分数直接下结论也是一种不错的方法。