0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AIZU ONLINE JUDGE 4データ構造

Posted at

3_A(スタック)

# include <bits/stdc++.h>
# define rep(i,n) for (int i=0; i<(n); i++)
using namespace std;
 
//topは最後の要素が格納されている場所を指す!
int top, S[1000];

//topを一つ増やし、S[top]にxを代入する!
void push(int x){
    S[++top]=x;
}

//S[top]の値を返し、topを一つ減らす
int pop(){
    top--;
    return S[top+1];
}

int main(){
    int a,b;
    top=0;
    char s[100];
    
    while(scanf("%s",s)!=EOF){
        if(s[0]=='+'){
            a=pop();
            b=pop();
            push(a+b);
        }else if(s[0]=='-'){
            b=pop();
            a=pop();
            push(a-b);
        }else if(s[0]=='*'){
            a=pop();
            b=pop();
            push(a*b);
        }else{
            push(atoi(s));//char型をint型に変換!
        }
    }
    
    printf("%d\n",pop());
    return 0;
}

3_B(キュー)

# include <bits/stdc++.h>
# define rep(i,n) for (int i=0; i<(n); i++)
# define LEN 100005
using namespace std;
 
typedef struct pp{
    char name[100];
    int t;
}P;

P Q[LEN];
int head,tail,n;

void enqueue(P x){
    Q[tail]=x;
    tail=(tail+1)%LEN;
}

P dequeue(){
    P x=Q[head];
    head=(head+1)%LEN;
    return x;
}

int min(int a, int b){ return a<b ? a:b;}

int main(){
    int elaps=0,c;
    int i,q;
    P u;
    scanf("%d %d",&n,&q);
    
    for (int i=1; i<=n; i++){
        scanf("%s",Q[i].name);
        scanf("%d",&Q[i].t);
    }
    head =1; tail=n+1;
    
    while(head!=tail){
        u=dequeue();
        c=min(q,u.t);
        u.t-=c;
        elaps+=c;
        if(u.t>0) enqueue(u);
        else{
            printf("%s %d\n",u.name,elaps);
        }
    }
    
    return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?