QFNU-ACM 2020.11.6 Trating

D

题意:就是根据题意画出那样的图形

题解:因为n给了1-9所以可以使用打表,不然的话就是遍历然后输入空格,慢慢遍历就可,注意结尾没有空格。

题解:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    int count1=0;
    cin>>n;
    for(int i=1;i<=n+1;i++){
        int count=-1;
        for(int j=0;j<=n-i;j++){
            cout<<"  ";
        } 
        for(int j=0;j<i*2-1;j++){
            if(j>=i){
                count--;
                if(count==0){
                    cout<<count;
                }
                else{
                    cout<<count<<" ";
                }
            }
            else{
                count++;
                if(i==1){
                    cout<<count;
                }
                else{
                    cout<<count<<" ";    
                }    
            }
        }
        cout<<endl;
    }
    for(int i=n;i>=1;i--){
        int count=-1;
        for(int j=0;j<=n-i;j++){
            cout<<"  ";
        } 
        for(int j=0;j<i*2-1;j++){
            if(j>=i){
                count--;
                if(count==0){
                    cout<<count;
                }
                else{
                    cout<<count<<" ";
                }
            }
            else{
                count++;
                if(i==1){
                    cout<<count;
                }
                else{
                    cout<<count<<" ";    
                }
            }
        }
        cout<<endl;
    }
}

7-11 彩虹瓶 (25分)

题意:给定一个序列,要求按顺序把不同的颜色拿出来。

题解:使用stack的知识,然后如果不是这个颜色就把他加入栈,不然就把这个颜色拿出来,最后判断栈里面的元素有没有箱子的长度少。

代码:

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
    int N,M,K;
    int a;
    scanf("%d %d %d",&N,&M,&K);
    while(K--){//如果插入的数跟开始一样就把开始的数++(因为取出来了)
    //再判断下面一个相不相等,不相等的话就break 
        stack<int> s1;
        int begin=1,flag=0;
        for(int j=0;j<N;j++){
            scanf("%d",&a);
            if(a==begin){  
                begin++;
                while(s1.size()){ 
                    if(s1.top() == begin){
                        s1.pop();
                        begin++;
                    }
                    else  break;
                }
            }else{
                s1.push(a);
                if(s1.size()>M) flag=1;
            }
        }
        if(s1.size()==0&&flag==0) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

posted @ 2020-11-15 22:45  liyongqishiwo  阅读(143)  评论(0编辑  收藏  举报