【倒计时5day 31/155】

【倒计时5day 31/155】

1091 Acute Stroke

#include<iostream>
#include<queue>
using namespace std;
const int M=1286,N=128,L=60;
int G[L][M][N];
int m,n,l,T;
struct Node{
    int x,y,z;
};
int direction[][3]={
    {1,0,0},
    {-1,0,0},
    {0,1,0},
    {0,-1,0},
    {0,0,1},
    {0,0,-1},
};

int bfs(int x,int y,int z){
    queue<Node> q;
    q.push({x,y,z});
    G[x][y][z]=0;
    int cnt=1;
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<6;i++){
            int a=t.x+direction[i][0];
            int b=t.y+direction[i][1];
            int c=t.z+direction[i][2];
            if(a>=0&&a<l&&b>=0&&b<m&&c>=0&&c<n&&G[a][b][c]){
                cnt++;
                q.push({a,b,c});
                G[a][b][c]=0;
            }
        }
    }
    return cnt;
}
int main(){
    scanf("%d%d%d%d",&m,&n,&l,&T);
    for(int i=0;i<l;i++){
        for(int j=0;j<m;j++){
            for(int k=0;k<n;k++){
                scanf("%d",&G[i][j][k]);
            }
        }
    }
    int res=0;
     for(int i=0;i<l;i++){
        for(int j=0;j<m;j++){
            for(int k=0;k<n;k++){
                if(G[i][j][k]){
                    int t=bfs(i,j,k);
                    if(t>=T){
                        res+=t;
                    }
                }
            }
        }
    }
    printf("%d\n",res);
    return 0;
}

1051 Pop Sequence

#include<iostream>
#include<stack>
#define MaxN 1010
using namespace std;
int M,N,K;
int a[MaxN];
bool check(){
    stack<int> st;
    for(int i=1,j=0;i<=N;i++){
        st.push(i);//入栈
        if(st.size()>M){
            return false;
        }
        while(st.size()&&st.top()==a[j]){//把能弹出来的弹出来
            st.pop();
            j++;
        }
    }
    return st.empty();//最后没有弹完,不能形成
}
int main(){
    scanf("%d%d%d",&M,&N,&K);
    int temp;
    for(int i=0;i<K;i++){
        for(int j=0;j<N;j++){
            scanf("%d",&a[j]);
        }
        bool success=check();
        if(success){
            printf("YES\n");
        }else{
            printf("NO\n");
        }
    }
    return 0;
}

1055 The World’s Richest

#include<iostream>
#include<algorithm>
using namespace std;
#define MaxN 100010
int N,M;
struct person{
    string name;
    int age;
    int worth;
    bool operator<(const person& t){
        if(worth>t.worth){
            return true;
        }else if(worth==t.worth){
            if(age<t.age){
                return true;
            }else if(age==t.age){
                if(name<t.name){
                    return true;
                }
            }
        }
        return false;
    }
}p[MaxN];
int main(){
    scanf("%d%d",&N,&M);
    for(int i=0;i<N;i++){
        cin>>p[i].name>>p[i].age>>p[i].worth;
    }
    sort(p,p+N);
    int num,mina,maxa;
    for(int i=1;i<=M;i++){
        scanf("%d%d%d",&num,&mina,&maxa);
        printf("Case #%d:\n",i);
        int temp=num;
        for(int j=0;j<N&&temp;j++){
            if(p[j].age>=mina&&p[j].age<=maxa){
                printf("%s %d %d\n",p[j].name.c_str(),p[j].age,p[j].worth);
                temp--;
            }
        }
        if(temp==num){//没有符合的
            printf("None\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,线程倒计时可以通过多种方式实现,包括使用postDelayed、runOnUiThread和AsyncTask。 1. 使用postDelayed方式实现线程倒计时: ``` final Handler handler = new Handler(); handler.postDelayed(new Runnable() { int count = 10; @Override public void run() { // 更新UI tvCountDown.setText("" + count); count--; if (count >= 0) { handler.postDelayed(this, 1000); // 一秒后再次执行 } else { // 倒计时结束 } } }, 1000); // 延迟一秒后执行 ``` 2. 使用runOnUiThread方式实现线程倒计时: ``` new Thread(new Runnable() { int count = 10; @Override public void run() { while (count >= 0) { runOnUiThread(new Runnable() { @Override public void run() { // 更新UI tvCountDown.setText("" + count); } }); try { Thread.sleep(1000); // 暂停一秒 } catch (InterruptedException e) { e.printStackTrace(); } count--; } // 倒计时结束 } }).start(); ``` 3. 使用AsyncTask方式实现线程倒计时: ``` private class CountDownTask extends AsyncTask<Void, Integer, Void> { @Override protected Void doInBackground(Void... voids) { int count = 10; while (count >= 0) { publishProgress(count); try { Thread.sleep(1000); // 暂停一秒 } catch (InterruptedException e) { e.printStackTrace(); } count--; } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); // 更新UI tvCountDown.setText("" + values[0]); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); // 倒计时结束 } } // 启动AsyncTask new CountDownTask().execute(); ``` 需要注意的是,以上三种实现方式都有可能会出现卡顿的情况,特别是在倒计时时间较长的情况下。为了避免卡顿,可以考虑使用CountDownTimer类来实现线程倒计时

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值