试题编号: | 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正好出现一次且为最后一个数字。 |
java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = 0;
int sum = 0;
int a = 0;
while((a = sc.nextInt()) != 0) {
if(a == 1) {
sum += 1;
k = 0;
}else if(a == 2){
sum += 2 * (++k);
}
}
System.out.println(sum);
}
}
c++:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int sum=0,pre=1;
do{
int temp;cin>>temp;
if(temp==0) break;
else if(temp==1) {pre=1;sum+=1;}
else{
if(pre<2){
pre=2;
}
else{
pre+=2;
}
sum+=pre;
}
}while(true);
cout<<sum;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=1,score=0,num=0;//a存储输入数据,score为最终得分,num为连跳方块中心次数
while(~scanf("%d",&a)&&a!=0)//数据输入还没完成且a!=0
if(a==1){//如果a==1
score+=a;//加上1分
num=0;//连跳方块中心次数归零
}else if(a==2)//a==2
score+=2*(++num);//递增连跳方块中心次数,得分为该次数乘2
printf("%d",score);
return 0;