Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

打多了一個 ! ,結果WA了

Posted by miklcct at 2011-06-22 15:56:23 on Problem 2849
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    if (argc > 1) {
        freopen("2849.in", "r", stdin);
    }
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        char tape[128001];
        {
            // read the tape
            char *p = tape;
            int end_count = 0;
            const char end_string[] = "\nend\n";
            bool commented = false;
            while (end_string[end_count]) {
                int c = getchar();
                if (c == end_string[end_count]) {
                    ++end_count;
                } else {
                    end_count = 0;
                }
                if (c == '%') {
                    commented = true;
                }
                if (c == '\n') {
                    commented = false;
                }
                if (!commented && strchr("><+-.[]", c)) {
                    *p++ = c;
                }
            }
            *p = '\0';
        }
        bool well_formed = true;
        {
            // check if the tape is well-formed
            int count = 0;
            for (char *p = tape; *p; ++p) {
                if (*p == '[') {
                    ++count;
                }
                if (*p == ']') {
                    --count;
                    if (count < 0) {
                        well_formed = false;
                    }
                }
            }
            if (count) {
                well_formed = false;
            }
        }
        printf("PROGRAM #%d:\n", i + 1);
        if (well_formed) {
            // run the program
            unsigned char ram[32768] = {};
            const char *stack[sizeof tape], **sp = stack;
            unsigned char *dp = ram;
            for (const char *ip = tape; *ip; ++ip) {
                switch (*ip) {
                  case '>':
                    ++dp;
                    if (dp == (&ram)[1]) dp = ram;
                    break;
                  case '<':
                    if (dp == ram) dp = (&ram)[1];
                    --dp;
                    break;
                  case '+':
                    ++*dp;
                    break;
                  case '-':
                    --*dp;
                    break;
                  case '.':
                    putchar(*dp);
                    break;
                  case '[':
                    if (!*dp) {
                        ++ip;
                        int count = 0;
                        while (count || *ip != ']') {
                            if (*ip == '[') {
                                ++count;
                            }
                            if (*ip == ']') {
                                --count;
                            }
                            ++ip;
                        }
                    } else {
                        *sp++ = ip;
                    }
                    break;
                  case ']':
                    if (*dp) {
                        ip = sp[-1];
                    } else {
                        --sp;
                    }
                    break;
                  default:
                    assert(false);
                }
            }
            putchar('\n');
        } else {
            puts("COMPILE ERROR");
        }
    }
    return 0;
}

我在處理 [ 的時候把 while 中的 count 打了成 !count ,結果WA了。

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator