第十一届蓝桥杯省赛C++B组 REPEAT 程序

在这里插入图片描述
题目给出的prog.txt文件:

A = 0
REPEAT 2:
    A = A + 4
    REPEAT 5:
        REPEAT 6:
            A = A + 5
        A = A + 7
    REPEAT 6:
        A = A + 7
        REPEAT 4:
            A = A + 2
            A = A + 7
        A = A + 2
    REPEAT 7:
        REPEAT 4:
            A = A + 8
            A = A + 7
            A = A + 4
            A = A + 5
        A = A + 8
    REPEAT 8:
        A = A + 5
        REPEAT 1:
            A = A + 2
        REPEAT 7:
            A = A + 5
        A = A + 5
    REPEAT 2:
        REPEAT 3:
            A = A + 1
        A = A + 1
    REPEAT 5:
        A = A + 1
    REPEAT 9:
        REPEAT 6:
            A = A + 5
            A = A + 1
        REPEAT 6:
            A = A + 2
            A = A + 8
            A = A + 3
        REPEAT 2:
            A = A + 5
        REPEAT 3:
            A = A + 9
        REPEAT 1:
            A = A + 4
        REPEAT 2:
            A = A + 9
        REPEAT 1:
            A = A + 6
            A = A + 6
            A = A + 4
        REPEAT 3:
            A = A + 7
        A = A + 1
        REPEAT 2:
            A = A + 3
        REPEAT 5:
            A = A + 2
            A = A + 5
            A = A + 2
        A = A + 4
    A = A + 3
REPEAT 4:
    A = A + 4
    A = A + 3
    A = A + 7
    REPEAT 5:
        REPEAT 4:
            A = A + 5
            A = A + 7
        REPEAT 5:
            A = A + 3
        REPEAT 3:
            A = A + 3
            A = A + 1
        A = A + 8
        A = A + 2
        REPEAT 9:
            A = A + 5
        REPEAT 1:
            A = A + 5
        A = A + 2
    A = A + 8
A = A + 6
REPEAT 3:
    REPEAT 4:
        A = A + 9
        REPEAT 5:
            A = A + 2
        A = A + 1
    REPEAT 9:
        A = A + 9
        A = A + 2
        REPEAT 1:
            A = A + 6
            A = A + 8
        REPEAT 2:
            A = A + 9
            A = A + 4
        A = A + 7
    REPEAT 2:
        REPEAT 7:
            A = A + 3
            A = A + 5
        REPEAT 3:
            A = A + 5
            A = A + 3
            A = A + 6
            A = A + 4
        REPEAT 9:
            A = A + 2
            A = A + 8
            A = A + 2
        A = A + 3
    REPEAT 2:
        REPEAT 8:
            A = A + 5
            A = A + 1
        A = A + 6
        A = A + 1
    A = A + 2
    REPEAT 6:
        REPEAT 1:
            A = A + 3
        REPEAT 1:
            A = A + 2
        REPEAT 4:
            A = A + 7
            A = A + 1
        A = A + 8
        REPEAT 6:
            A = A + 5
        REPEAT 6:
            A = A + 3
        REPEAT 2:
            A = A + 2
            A = A + 9
        A = A + 7
    REPEAT 9:
        A = A + 8
        REPEAT 9:
            A = A + 8
            A = A + 9
            A = A + 3
        A = A + 2
        REPEAT 6:
            A = A + 3
        REPEAT 9:
            A = A + 1
        A = A + 9
        A = A + 5
        REPEAT 2:
            A = A + 4
            A = A + 9
        A = A + 8
        REPEAT 5:
            A = A + 6
            A = A + 9
        A = A + 1
    REPEAT 1:
        A = A + 4
    A = A + 2
    REPEAT 9:
        REPEAT 3:
            A = A + 4
        REPEAT 7:
            A = A + 8
            A = A + 3
        REPEAT 5:
            A = A + 9
        REPEAT 8:
            A = A + 9
            A = A + 8
        REPEAT 4:
            A = A + 7
        A = A + 7
    A = A + 3
A = A + 5
REPEAT 6:
    A = A + 7
REPEAT 7:
    A = A + 2
    A = A + 2
A = A + 1
REPEAT 8:
    REPEAT 1:
        REPEAT 4:
            A = A + 6
            A = A + 6
            A = A + 2
        REPEAT 5:
            A = A + 4
            A = A + 8
            A = A + 4
        REPEAT 1:
            A = A + 5
        REPEAT 7:
            A = A + 8
        REPEAT 6:
            A = A + 4
        A = A + 4
        A = A + 8
        REPEAT 4:
            A = A + 2
        REPEAT 2:
            A = A + 4
        REPEAT 2:
            A = A + 3
        REPEAT 1:
            A = A + 2
        A = A + 8
        REPEAT 2:
            A = A + 7
        REPEAT 8:
            A = A + 6
            A = A + 1
        A = A + 7
    REPEAT 8:
        A = A + 2
    REPEAT 8:
        REPEAT 6:
            A = A + 1
            A = A + 6
        REPEAT 2:
            A = A + 4
            A = A + 1
        A = A + 7
    A = A + 4
REPEAT 4:
    REPEAT 9:
        A = A + 2
    REPEAT 1:
        A = A + 2
    A = A + 5
REPEAT 8:
    REPEAT 6:
        A = A + 3
    REPEAT 4:
        A = A + 1
        A = A + 6
        A = A + 1
    REPEAT 7:
        A = A + 7
        REPEAT 7:
            A = A + 3
            A = A + 9
            A = A + 1
            A = A + 9
        REPEAT 3:
            A = A + 5
        A = A + 5
        A = A + 6
        A = A + 2
    REPEAT 1:
        A = A + 4
    REPEAT 2:
        A = A + 7
    REPEAT 1:
        A = A + 7
        REPEAT 4:
            A = A + 7
        A = A + 2
        REPEAT 5:
            A = A + 9
            A = A + 1
            A = A + 9
            A = A + 5
            A = A + 9
        REPEAT 5:
            A = A + 5
        REPEAT 1:
            A = A + 6
        REPEAT 2:
            A = A + 3
            A = A + 2
            A = A + 6
            A = A + 8
            A = A + 8
            A = A + 7
        A = A + 5
    A = A + 5
REPEAT 2:
    A = A + 1
    A = A + 7
A = A + 3
REPEAT 2:
    A = A + 7
A = A + 1
A = A + 4
REPEAT 1:
    REPEAT 7:
        REPEAT 2:
            A = A + 3
            A = A + 5
            A = A + 2
        A = A + 6
    A = A + 1
A = A + 2
A = A + 4
A = A + 9
REPEAT 1:
    A = A + 8
REPEAT 8:
    REPEAT 4:
        REPEAT 8:
            A = A + 4
        REPEAT 3:
            A = A + 1
            A = A + 8
        REPEAT 7:
            A = A + 8
        REPEAT 7:
            A = A + 7
        A = A + 7
        REPEAT 7:
            A = A + 6
        REPEAT 5:
            A = A + 9
        A = A + 3
        REPEAT 4:
            A = A + 5
        A = A + 5
    A = A + 4
    REPEAT 9:
        REPEAT 3:
            A = A + 4
            A = A + 3
            A = A + 6
        REPEAT 1:
            A = A + 3
            A = A + 3
            A = A + 6
        REPEAT 6:
            A = A + 7
            A = A + 7
            A = A + 5
            A = A + 5
            A = A + 1
            A = A + 2
        A = A + 6
        A = A + 6
    REPEAT 9:
        A = A + 6
    REPEAT 1:
        REPEAT 2:
            A = A + 4
            A = A + 7
        REPEAT 3:
            A = A + 6
        REPEAT 5:
            A = A + 3
        A = A + 6
        REPEAT 9:
            A = A + 3
        A = A + 6
    REPEAT 5:
        A = A + 8
        A = A + 8
        REPEAT 3:
            A = A + 7
            A = A + 9
            A = A + 8
            A = A + 3
        A = A + 3
    A = A + 9
REPEAT 6:
    A = A + 9
A = A + 1
REPEAT 4:
    REPEAT 1:
        A = A + 7
    REPEAT 9:
        A = A + 2
        A = A + 9
    A = A + 1
A = A + 2
A = A + 8
A = A + 7
A = A + 9
A = A + 6
REPEAT 4:
    REPEAT 2:
        A = A + 3
    REPEAT 3:
        A = A + 4
    A = A + 4
REPEAT 6:
    A = A + 6
A = A + 1
A = A + 5
A = A + 8
REPEAT 2:
    A = A + 6
    REPEAT 1:
        REPEAT 2:
            A = A + 2
        REPEAT 3:
            A = A + 1
        REPEAT 1:
            A = A + 8
            A = A + 7
            A = A + 4
            A = A + 2
            A = A + 8
        A = A + 4
    REPEAT 5:
        REPEAT 6:
            A = A + 8
        REPEAT 9:
            A = A + 5
        A = A + 5
    REPEAT 5:
        A = A + 5
    REPEAT 3:
        REPEAT 5:
            A = A + 4
        REPEAT 4:
            A = A + 6
            A = A + 3
        REPEAT 7:
            A = A + 3
            A = A + 3
            A = A + 1
            A = A + 7
            A = A + 7
            A = A + 6
            A = A + 5
            A = A + 5
        A = A + 6
    REPEAT 1:
        A = A + 9
    A = A + 3
    REPEAT 1:
        REPEAT 1:
            A = A + 1
        REPEAT 8:
            A = A + 5
        REPEAT 8:
            A = A + 6
        REPEAT 4:
            A = A + 9
        A = A + 4
    REPEAT 2:
        A = A + 3
        A = A + 7
        REPEAT 5:
            A = A + 7
            A = A + 5
            A = A + 8
        A = A + 7
        A = A + 8
    A = A + 5
    REPEAT 2:
        A = A + 5
    A = A + 7
    A = A + 8
A = A + 5
A = A + 9
REPEAT 2:
    REPEAT 6:
        A = A + 9
        A = A + 1
        A = A + 8
        A = A + 7
        A = A + 1
        A = A + 5
    REPEAT 3:
        A = A + 3
        A = A + 9
        A = A + 7
    REPEAT 3:
        A = A + 9
    A = A + 1
    REPEAT 6:
        A = A + 1
    REPEAT 9:
        REPEAT 7:
            A = A + 3
        REPEAT 5:
            A = A + 5
            A = A + 8
            A = A + 8
            A = A + 1
            A = A + 2
        REPEAT 4:
            A = A + 6
        REPEAT 3:
            A = A + 3
        A = A + 7
    REPEAT 8:
        REPEAT 1:
            A = A + 7
        A = A + 8
        A = A + 3
    A = A + 1
A = A + 2
A = A + 4
A = A + 7
REPEAT 1:
    REPEAT 1:
        REPEAT 1:
            A = A + 4
            A = A + 6
        REPEAT 1:
            A = A + 3
            A = A + 9
            A = A + 6
        REPEAT 9:
            A = A + 1
            A = A + 6
        REPEAT 5:
            A = A + 3
            A = A + 9
        A = A + 5
        A = A + 5
    A = A + 7
    A = A + 2
    REPEAT 2:
        A = A + 7
    A = A + 7
    REPEAT 7:
        REPEAT 4:
            A = A + 6
        A = A + 8
        REPEAT 6:
            A = A + 6
        REPEAT 2:
            A = A + 1
        A = A + 7
        A = A + 6
    A = A + 7
    REPEAT 4:
        REPEAT 7:
            A = A + 1
        REPEAT 2:
            A = A + 2
            A = A + 5
        A = A + 8
    A = A + 2
A = A + 1
A = A + 4
REPEAT 8:
    A = A + 5
A = A + 6
REPEAT 7:
    REPEAT 6:
        REPEAT 9:
            A = A + 7
            A = A + 8
        REPEAT 4:
            A = A + 6
            A = A + 4
            A = A + 3
            A = A + 6
        REPEAT 9:
            A = A + 3
        REPEAT 9:
            A = A + 2
        A = A + 7
    A = A + 5
    A = A + 2
REPEAT 7:
    REPEAT 8:
        REPEAT 6:
            A = A + 4
            A = A + 9
            A = A + 5
            A = A + 3
        A = A + 9
    REPEAT 4:
        REPEAT 1:
            A = A + 6
            A = A + 8
        REPEAT 1:
            A = A + 6
        A = A + 4
        A = A + 6
    REPEAT 3:
        A = A + 7
        REPEAT 3:
            A = A + 4
            A = A + 4
            A = A + 2
        A = A + 3
        A = A + 7
    REPEAT 5:
        A = A + 6
        A = A + 5
    REPEAT 1:
        REPEAT 8:
            A = A + 5
        REPEAT 3:
            A = A + 6
        REPEAT 9:
            A = A + 4
        A = A + 3
    REPEAT 6:
        REPEAT 2:
            A = A + 1
        A = A + 5
    A = A + 2
A = A + 2
A = A + 7
REPEAT 4:
    A = A + 7
A = A + 9
A = A + 2
REPEAT 8:
    A = A + 9
    REPEAT 9:
        REPEAT 2:
            A = A + 3
            A = A + 2
            A = A + 1
            A = A + 5
        REPEAT 9:
            A = A + 1
            A = A + 3
        A = A + 9
        REPEAT 7:
            A = A + 2
        REPEAT 5:
            A = A + 9
            A = A + 3
        REPEAT 2:
            A = A + 4
        REPEAT 8:
            A = A + 9
        REPEAT 5:
            A = A + 5
            A = A + 4
        A = A + 2
    A = A + 4
    REPEAT 6:
        A = A + 2
        REPEAT 5:
            A = A + 7
            A = A + 7
            A = A + 8
            A = A + 3
        REPEAT 8:
            A = A + 2
            A = A + 5
        REPEAT 1:
            A = A + 8
            A = A + 5
            A = A + 1
            A = A + 1
        A = A + 5
        REPEAT 2:
            A = A + 6
        REPEAT 6:
            A = A + 9
            A = A + 2
        A = A + 5
        REPEAT 4:
            A = A + 7
        A = A + 1
        REPEAT 6:
            A = A + 8
        A = A + 4
    REPEAT 3:
        REPEAT 2:
            A = A + 1
            A = A + 5
        REPEAT 2:
            A = A + 7
        REPEAT 9:
            A = A + 6
            A = A + 8
            A = A + 9
        A = A + 5
    REPEAT 9:
        REPEAT 3:
            A = A + 7
            A = A + 7
        A = A + 9
        A = A + 7
        REPEAT 5:
            A = A + 7
            A = A + 2
        A = A + 1
    A = A + 8
    A = A + 3
    A = A + 5
A = A + 1
REPEAT 8:
    A = A + 4
A = A + 2
A = A + 2
A = A + 8
REPEAT 4:
    REPEAT 4:
        A = A + 8
        REPEAT 7:
            A = A + 5
            A = A + 2
        REPEAT 2:
            A = A + 6
        REPEAT 4:
            A = A + 8
            A = A + 6
        A = A + 1
    A = A + 3
A = A + 2
A = A + 7
A = A + 4
REPEAT 8:
    A = A + 2
    A = A + 4
REPEAT 5:
    REPEAT 3:
        REPEAT 6:
            A = A + 8
            A = A + 1
        A = A + 6
    A = A + 5
    A = A + 9
REPEAT 8:
    A = A + 7
REPEAT 6:
    A = A + 4
A = A + 5
REPEAT 3:
    A = A + 1
    REPEAT 1:
        REPEAT 5:
            A = A + 6
        A = A + 2
    REPEAT 9:
        REPEAT 5:
            A = A + 9
            A = A + 3
        REPEAT 9:
            A = A + 9
        A = A + 8
    REPEAT 8:
        REPEAT 5:
            A = A + 9
            A = A + 4
        REPEAT 9:
            A = A + 3
        A = A + 4
    A = A + 5
REPEAT 9:
    REPEAT 7:
        A = A + 5
    REPEAT 3:
        A = A + 7
    REPEAT 9:
        REPEAT 6:
            A = A + 4
        A = A + 6
    REPEAT 5:
        REPEAT 6:
            A = A + 5
            A = A + 3
        A = A + 3
    A = A + 3
    A = A + 5
    REPEAT 7:
        A = A + 5
        REPEAT 2:
            A = A + 5
            A = A + 6
        REPEAT 2:
            A = A + 2
        A = A + 5
    A = A + 3
A = A + 5
A = A + 5
REPEAT 4:
    A = A + 2
    A = A + 1
    REPEAT 9:
        A = A + 9
        A = A + 5
        A = A + 6
        A = A + 2
        A = A + 2
        A = A + 5
    REPEAT 9:
        A = A + 5
    A = A + 4
    REPEAT 4:
        REPEAT 4:
            A = A + 1
            A = A + 2
        REPEAT 6:
            A = A + 9
            A = A + 3
        REPEAT 2:
            A = A + 5
            A = A + 1
            A = A + 1
            A = A + 3
        A = A + 8
        REPEAT 7:
            A = A + 4
        REPEAT 6:
            A = A + 9
        REPEAT 5:
            A = A + 9
            A = A + 8
            A = A + 3
        A = A + 9
        A = A + 4
    A = A + 6
REPEAT 7:
    A = A + 9
REPEAT 9:
    A = A + 4
    A = A + 9
    A = A + 1
    A = A + 3
    REPEAT 5:
        REPEAT 1:
            A = A + 4
            A = A + 4
        REPEAT 8:
            A = A + 9
            A = A + 6
            A = A + 2
        REPEAT 3:
            A = A + 4
            A = A + 4
        REPEAT 3:
            A = A + 5
            A = A + 2
            A = A + 8
            A = A + 3
            A = A + 6
            A = A + 4
            A = A + 9
            A = A + 1
        A = A + 9
        A = A + 5
        A = A + 3
        REPEAT 3:
            A = A + 2
            A = A + 5
            A = A + 8
            A = A + 2
        A = A + 5
    REPEAT 8:
        REPEAT 2:
            A = A + 6
        A = A + 7
    A = A + 6
    A = A + 9
    A = A + 2
REPEAT 2:
    A = A + 3
    REPEAT 8:
        A = A + 7
        A = A + 2
        A = A + 1
        A = A + 4
        A = A + 1
        A = A + 5
    A = A + 2
    A = A + 1
    REPEAT 1:
        A = A + 1
    REPEAT 6:
        A = A + 4
        A = A + 3
    A = A + 3
    REPEAT 5:
        A = A + 3
    REPEAT 6:
        REPEAT 1:
            A = A + 5
            A = A + 7
            A = A + 7
            A = A + 7
        REPEAT 5:
            A = A + 9
        A = A + 7
        REPEAT 5:
            A = A + 9
            A = A + 1
            A = A + 9
        A = A + 8
        REPEAT 1:
            A = A + 2
        REPEAT 5:
            A = A + 8
        REPEAT 3:
            A = A + 2
            A = A + 9
        A = A + 6
        A = A + 3
    REPEAT 5:
        REPEAT 6:
            A = A + 5
            A = A + 5
        REPEAT 4:
            A = A + 5
        A = A + 4
        REPEAT 8:
            A = A + 9
            A = A + 1
        REPEAT 8:
            A = A + 8
            A = A + 1
        A = A + 4
        REPEAT 6:
            A = A + 6
        REPEAT 2:
            A = A + 3
            A = A + 9
            A = A + 6
            A = A + 9
        REPEAT 1:
            A = A + 4
        REPEAT 3:
            A = A + 3
            A = A + 4
            A = A + 2
            A = A + 8
        REPEAT 2:
            A = A + 4
        A = A + 1
        REPEAT 9:
            A = A + 2
        A = A + 9
    A = A + 7
REPEAT 7:
    REPEAT 7:
        REPEAT 5:
            A = A + 7
        REPEAT 5:
            A = A + 1
        A = A + 1
    REPEAT 5:
        A = A + 6
        REPEAT 1:
            A = A + 4
        REPEAT 9:
            A = A + 4
        A = A + 1
    REPEAT 6:
        A = A + 8
        A = A + 5
        REPEAT 1:
            A = A + 4
        REPEAT 5:
            A = A + 8
            A = A + 7
        A = A + 2
    REPEAT 3:
        A = A + 3
    REPEAT 8:
        REPEAT 8:
            A = A + 4
        A = A + 7
        REPEAT 5:
            A = A + 1
        REPEAT 8:
            A = A + 7
            A = A + 8
            A = A + 4
        A = A + 7
        A = A + 6
    A = A + 9
    A = A + 5
REPEAT 3:
    A = A + 5
    REPEAT 9:
        A = A + 1
    A = A + 7
REPEAT 1:
    A = A + 8
A = A + 4
REPEAT 8:
    REPEAT 7:
        A = A + 2
        REPEAT 4:
            A = A + 6
        A = A + 6
    REPEAT 1:
        A = A + 7
    A = A + 1
REPEAT 9:
    REPEAT 5:
        A = A + 6
        A = A + 5
        REPEAT 7:
            A = A + 3
            A = A + 6
        A = A + 8
    REPEAT 2:
        A = A + 7
    A = A + 1
    A = A + 9
    REPEAT 3:
        REPEAT 3:
            A = A + 5

分析:
缩进量的多少代表循环包含的执行语句
碰到每次循环的语句开两个数组分别存这一行的缩进量和代表的循环次数

int w;

用w记录累计循环的次数,碰到缩进量比栈中少的语句,栈弹出,w对应相除
完整代码:

#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e3+10;
char mp[N][N];
int s[N],t[N],top;
int main()
{
    FILE *fp;
    fp=fopen("prog.txt","r");///文件读操作
    char ch;
    int tot=0;
    int i=0;
    while((ch=fgetc(fp))!=EOF)///一个字符一个字符的读入
    {
        
        if(ch=='\n')
        {
            mp[tot][i]='\0';
            tot++;
            i=0;
            continue;
        }
        mp[tot][i++]=ch;
    }
    /*
    for(int i=0;i<tot;i++)
        printf("%s\n",mp[i]);*/
    ///将txt中的数据已经全部保存在了mp数组中
    top=0;
    int p,w=1,sum=0;
    s[top]=-1,t[top]=-1;///防止栈空的时候进行出栈操作
    for(int i=0; i<tot; i++)
    {
        int len=strlen(mp[i]);
        p=0;
        while(mp[i][p]==' ')///统计缩进
            p++;
        while(s[top]>=p)///开始新的循环,上一层的循环不包括
            w/=t[top--];
        if(mp[i][len-1]==':')///当前是循环语句
        {
            int num=mp[i][len-2]-'0';
            top++;
            w*=num;///累计循环了几次
            s[top]=p,t[top]=num;///第top层循环语句的缩进量和循环次数
        }
        else
        {
            int num=mp[i][len-1]-'0';
            sum+=w*num;
        }
    }
    printf("%d\n",sum);

    return 0;
}
//241830

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页