编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#1880 #993. 汉诺塔问题 Compile Error 0 0 ms 0 K C++ 17 / 971 B t330026143 2023-10-15 16:58:46
显示原始代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 31;
int st[3][maxn], top[3];
int n = 6, last = -1, judge;
int priority[6][2];
char ch[10];
void solve(int step) {
    if (top[1] == 3 || top[2] == 3) {
        judge = step;
        return;
    }
    for (int i = 0; i < 6; i++) {
        int p1 = priority[i][0], p2 = priority[i][1];
        if (top[p1] == 0 || st[p1][top[p1]] == last)
            continue;
        if (top[p2] == 0 || st[p1][top[p1]] < st[p2][top[p2]]) {
            int t = st[p1][top[p1]--];
            st[p2][++top[p2]] = t;
            last = t;
            break;
        }
    }
    solve(step + 1);
}
int main() {
    scanf("%d\n", &n);
    for (int i = 0; i < 6; i++) scanf("%s", ch), priority[i][0] = ch[0] - 'A', priority[i][1] = ch[1] - 'A';
    for (int i = 3; i >= 1; i--) st[0][++top[0]] = i;
    solve(0);  //这里判断情况
    if (judge == 7) {
        long long ans = (1 << n) - 1;
    } else if (judge == 17) {
        long long ans = 2;
        for (int i = 1; i < n; i++) ans *= 3;
        ans--;
    } else if (judge == 9) {
        long long ans = 1;
        for (int i = 1; i < n; i++) ans *= 3;
    }
    printf("%lld\n", ans);
}

编译信息

/sandbox/1/a.cpp: In function 'int main()':
/sandbox/1/a.cpp:50:21: error: 'ans' was not declared in this scope; did you mean 'abs'?
   50 |     printf("%lld\n",ans);
      |                     ^~~
      |                     abs
/sandbox/1/a.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |  scanf("%d\n",&n);
      |  ~~~~~^~~~~~~~~~~
/sandbox/1/a.cpp:32:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |  for(int i=0;i<6;i++)scanf("%s",ch),priority[i][0]=ch[0]-'A',priority[i][1]=ch[1]-'A';
      |                      ~~~~~^~~~~~~~~