第十三次csp 1、2题

1

//13CSP A
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){

    int jump;
    int score = 0,cnt = 0,pre = 0;
    while(true){
        cin>>jump;

        if(jump == 1){
            if(pre == 2){
                pre = 1;
                cnt = 0;
            }
            score++;
            //pre = 1;
        }
        else if(jump == 2){
            pre = 2;
            cnt ++;
            score += 2*cnt;
        }
        else if(jump == 0)break;
    }
    cout<< score <<endl;
    return 0;
}      

//1 1 2 2 2 1 1 2 2 0 

2

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct Ball{
  int pos;
  int direction;
  int id;
  bool operator <(const Ball &a) const{
    return pos < a.pos;
  }
};
bool cmp(Ball a,Ball b){
  return a.id < b.id;
}
Ball ball[105];
int main(){
  int n,l,t;
  cin>> n >> l >> t;

  for(int i = 1;i <= n;i ++){
    cin>> ball[i].pos;
    ball[i].direction = 1;//1 right / -1 left
    ball[i].id = i;
  } 
  sort(ball + 1,ball + n + 1);//数据增序排序

  for(int i = 1;i <= t;i ++){
    for(int j = 1;j <= n;j ++){
      ball[j].pos += ball[j].direction;
    }
    /*for(int i = 1;i <= n;i ++){
    cout<< ball[i].pos<< " ";
    } cout<<endl;*/
    for(int j = 1;j <= n;j ++){
      //ball[j].pos += ball[j].direction;
      if(ball[j].pos == 0)
        ball[j].direction = 1;
      if(ball[j].pos == l)
        ball[j].direction = -1;
      if(ball[j].pos == ball[j-1].pos){
        //swap(ball[j].direction,ball[j-1].direction);
        ball[j].direction = 1;//这里容易错的,需要注意只需要判断一遍就可以
        ball[j-1].direction = -1;
      }
      /*f(ball[j].pos == ball[j+1].pos){//
        swap(ball[j].direction,ball[j+1].direction);
      }*/
    }
  }
  sort(ball + 1,ball + n + 1,cmp);//还原成原来的顺序
  for(int i = 1; i <= n;i ++){
    cout<< ball[i].pos << (i==n? '\n':' ');
  }
  return 0;
}     



/*
3 10 5
4 6 8
10 22 30
14 12 16 6 10 2 8 20 18 4


 */    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值