现在的位置: 首页 > 综合 > 正文

POJ 1472

2017年11月21日 ⁄ 综合 ⁄ 共 1388字 ⁄ 字号 评论关闭
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char command[117];
char opnum[10117];

int solve(int exps[]){
    memset(command, 0, sizeof(command));
    scanf("%s", command);
    if(command[0] == 'E'){
        return 0;
    }
    else if(command[0] == 'B'){
        while(solve(exps));
    }
    else if(command[0] == 'L'){
        int i, t = -1;
        int texps[20] = {0};
        scanf("%s", opnum);
        if(opnum[0] != 'n'){
            t = atoi(opnum);
        }
        while(solve(texps));
        if(t == -1){
            for(i = 10; i > 0; i--){
                texps[i] = texps[i - 1];
            }
            texps[0] = 0;
        }
        else{
            for(i = 0; i <= 10; i++){
                texps[i] *= t;
            }
        }
        for(i = 0; i <= 10; i++){
            exps[i] += texps[i];
        }
    }
    else if(command[0] == 'O'){
        scanf("%s", opnum);
        int t = atoi(opnum);
        exps[0] += t;
        return solve(exps);
    }
return 1;
}

int main(){
    int k, i, j, mflag, mcount = 1;
    int exps[20];
    while(scanf("%d", &k) != EOF){
        while(k--){
            memset(exps, 0, sizeof(exps));
            mflag = 0;
            solve(exps);
            printf("Program #%d\n", mcount++);
            printf("Runtime = ");
            for(i = 11; i >= 2; i--){
                if(exps[i] != 0 && mflag != 0){
                    if(exps[i] != 1){
                        printf("+%d*n^%d", exps[i], i);
                    }
                    else{
                        printf("+n^%d", i);
                    }
                }
                else if(mflag == 0 && exps[i] != 0){
                    mflag = 1;
                    if(exps[i] != 1){
                        printf("%d*n^%d", exps[i], i);
                    }
                    else{
                        printf("n^%d", i);
                    }
                }
            }
            if(exps[1] != 0 && mflag != 0){
                if(exps[1] != 1){
                    printf("+%d*n", exps[1]);
                }
                else{
                    printf("+n");
                }
            }
            else if(exps[1] != 0 && mflag == 0){
                mflag = 1;
                if(exps[1] != 1){
                    printf("%d*n", exps[1]);
                }
                else{
                    printf("+n");
                }
            }
            if(exps[0] != 0 && mflag != 0){
                printf("+%d", exps[0]);
            }
            else if(exps[0] != 0 && mflag == 0){
                mflag = 1;
                printf("%d", exps[0]);
            }
            if(mflag == 0){
                printf("0");
            }
            printf("\n\n");
        }
    }
return 0;
}

递归模拟,代码写的很烂还参考了网上的=_=

【上篇】
【下篇】

抱歉!评论已关闭.