模拟小记

【p1098】字符串展开

【p1042】乒乓球

做模拟的lz日渐崩溃;

请放一首“我要一步一步往上爬”

【p1098】字符串展开

这个题的ac经历:50-60-70-80-100;

中间还夹杂了几次忘记关freopen的0;

模拟是魔鬼!

思路到是莫得什么好讲的,就是注意以下几个点:

1.连续的‘-’直接输出

2.‘-’出现在开头或末尾直接输出

3.当‘-’两端的字符分别是数字和字母时,原样输出。

没有然后:

#include<bits/stdc++.h>

using namespace std;

inline int read() {
    int ans=0;
    char last=' ',ch=getchar();
    while(ch>'9'||ch<'0') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

int p1,p2,p3;
char a[200];

int main() {
    p1=read();
    p2=read();
    p3=read();
    p3--;
    scanf("%s",a);
    int len=strlen(a);
    for(int i=0; i<len; i++) {
        if(a[i]=='-') {
            if(i==0||i==len-1){
                printf("-");
                continue;
            }
            if(a[i+1]=='-'||a[i-1]=='-'){
                printf("-");
                continue;
            }
            char before=a[i-1],after=a[i+1];
            if(before>=after) {
                printf("%c",a[i]);
                continue;
            }
            if(before+1==after)    continue;
            if((before>='0'&&before<='9'&&(after>'9'||after=='-'))||(after>='0'&&after<='9'&&(before>'9'||before=='-'))) {

                printf("%c",a[i]);
                continue;
            }
            if(before>='0'&&before<='9'&&after>='0'&&after<='9') {
    
                if(p1==3) {
                    int x=after-before;
                    x--;
                    x*=p2;
                    for(int i=1; i<=x; i++)
                        printf("*");
                } else {
                    if(p3) {
                        int y=after-before;
                        y--;
                        char z=after-1;
                        int cnt=0;
                        for(int i=1; i<=y*p2; i++) {
                            if(cnt==p2) {
                                cnt=0;
                                z-=1;
                            }
                            printf("%c",z);
                            cnt++;
                        }
                    } else {
                        int y=after-before;
                        y--;
                        char z=before+1;
                        int cnt=0;
                        for(int i=1; i<=y*p2; i++) {
                            if(cnt==p2) {
                                cnt=0;
                                z+=1;
                            }
                            printf("%c",z);
                            cnt++;
                        }
                    }
                }
            }
            if(before>='a'&&before<='z'&&after>-'a'&&after<='z') {
                if(p1==3) {
                    int x=after-before;
                    x--;
                    x*=p2;
                    for(int i=1; i<=x; i++)
                        printf("*");
                } 
                if(p1==1) {
                    if(p3) {
                        int y=after-before;
                        y--;
                        char z=after-1;
                        int cnt=0;
                        for(int i=1; i<=y*p2; i++) {
                            if(cnt==p2) {
                                cnt=0;z-=1;
                            }
                            printf("%c",z);cnt++;
                        }
                    } else {
                        int y=after-before;
                        y--;
                        char z=before+1;
                        int cnt=0;
                        for(int i=1; i<=y*p2; i++) {
                            if(cnt==p2) {
                                cnt=0;z+=1;
                            }
                            printf("%c",z);cnt++;
                        }
                    }
                }
                if(p1==2){
                    if(p3) {
                        int y=after-before;y--;
                        char z=after-33;
                        int cnt=0;
                        for(int i=1; i<=y*p2; i++) {
                            if(cnt==p2) {
                                cnt=0;z-=1;
                            }
                            printf("%c",z);cnt++;
                        }
                    } else {
                        int y=after-before;    y--;
                           char z=before-31;
                        int cnt=0;
                        for(int i=1; i<=y*p2; i++) {
                            if(cnt==p2) {
                                cnt=0;z+=1;
                            }
                            printf("%c",z);cnt++;
                        }
                    }
                }
            }
        } else printf("%c",a[i]);
    }
    return 0;
}

 【p1042】乒乓球

恭喜lz喜提乒乓球11分制与21分制的差别;

反正这个题讲的不是很清楚;

11分制:

当有一方的分数达到11分并且二者的分差>=2时,就判定得11分的那一方胜出,因此可以输出一次比分。当某一方分数达到11分但是二者分差<2时,继续比赛,直到二者分差>=2;

21分制与11分制的规则大致一样,只不过是将11分改为21分;

然后注意特判E在开头出现的情况:EWWW……,直接输出0:0 0:0;

然后我是单个字符读入的,用了一个bool数组a来记录是W(1华华赢)还是L(0对手赢),然后从前到后扫描,当a[i]=1时,hh++;否则ds++;

然后输出的条件是:华华和对手有一方的分数>=11并且它们分数之差的绝对值>=2,这样就可以一概将楼上比分制度出现的所有情况都覆盖到了;

#include<bits/stdc++.h>

using namespace std;

bool a[62511];
int hh,ds;

int main() {
    int cnt=0;
    char ch;
    while(ch!='E') {
        ch=getchar();
        if(ch=='W') a[++cnt]=1;
        if(ch=='L') a[++cnt]=0;
    }
    if(cnt==0) printf("0:0

0:0");
    for(int i=1; i<=cnt; i++) {
        if(a[i]) hh++;
        else ds++;
        if((hh>=11||ds>=11)&&abs(hh-ds)>=2) {
            printf("%d:%d
",hh,ds);
            hh=ds=0;
        }
        if(i==cnt) printf("%d:%d
",hh,ds);
    }
    printf("
");
    hh=ds=0;
    for(int i=1; i<=cnt; i++) {
        if(a[i]) hh++;
        else ds++;
        if((hh>=21||ds>=21)&&abs(hh-ds)>=2) {
            printf("%d:%d
",hh,ds);
            hh=ds=0;
        }

        if(i==cnt&&i%21!=0) printf("%d:%d",hh,ds);
    }
    return 0;
}

end-

原文地址:https://www.cnblogs.com/zhuier-xquan/p/11121722.html