为什么USACO分级师不接受我的"Prime Cryptarithm"解决方案?(我的电脑和在线评分机之间的输出差异)

Why does the USACO grader not accept my solution to "Prime Cryptarithm"?(Output difference between my computer and the online grader)

本文关键字:我的 之间 在线 输出 不接受 USACO Prime Cryptarithm 为什么 解决方案 电脑      更新时间:2023-10-16

我正在编写解决问题的解决方案 http://acmph.blogspot.com/2010/12/usaco-prime-cryptarithm.html

当我在计算机上运行它时,给定输入数据

7
4 1 2 5 6 7 3

我的电脑给了我一个384的答案,这是正确的答案。 但是当我将其上传到USACO站点时,它说我的程序输出1。 为什么会这样?这是我的代码:

/*
ID: harry47341
PROG: crypt1
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> digits;
int a, b, c, d, e, abc, de;
bool oneof1(int n) {
    int count1 = 1;
    while (true) {
        if (count1 > n)break;
        else {
            count1 = count1 * 10;
            int digit = n%count1 / (count1 / 10);
            if (!count(digits.begin(), digits.end(), digit) ){
                return false;
            }
            n = n - n%count1;
        }
    }
}
bool check() {
    int n = abc*e;
    int n1 = abc*d;
    if (n > 999 || n < 100)return false;
    if (n1 > 999 || n1 < 100)return false;
    if ((n1 * 10 + n) > 9999 || (n1 * 10 + n) < 1000)return false;
    if (oneof1(n) && oneof1(n1) && oneof1(n1 * 10 + n)) {
        return true;
    }
}
int main() {
    ofstream fout("crypt1.out");
    ifstream fin("crypt1.in");
    int count = 0;
    int n;
    fin >> n;
    for (int i = 0; i < n; i++) {
        int f;
        fin >> f;
        digits.push_back(f);
    }
    for (int i = 0; i < n; i++) {
        a = digits[i];
        for (int i1 = 0; i1 < n; i1++) {
            b = digits[i1];
            for (int i2 = 0; i2 < n; i2++) {
                c = digits[i2];
                for (int i3 = 0; i3 < n; i3++) {
                    d = digits[i3];
                    for (int i4 = 0; i4 < n; i4++) {
                        e = digits[i4];
                        abc = a * 100 + b * 10 + c;
                        de = d * 10 + e;
                        if (check()) {
                            count++;
                        }
                    }
                }
            }
        }
    }
    fout << count << endl;
    return 0;
}

oneof1check 函数中,不会为函数中的所有返回路径返回值。 因此,您的程序表现出未定义的行为,因为从返回值的函数不返回任何内容是 UB。 这就是为什么在不同的计算机/系统上运行时您会得到不同的答案。

若要解决此问题,请在 oneof1 函数中返回 true,然后返回check函数的false

bool oneof1(int n) {
    int count1 = 1;
    while (true) {
        if (count1 > n)break;
        else {
            count1 = count1 * 10;
            int digit = n%count1 / (count1 / 10);
            if (!count(digits.begin(), digits.end(), digit) ){
                return false;
            }
            n = n - n%count1;
        }
    }
    return true;  // this was missing
}
 bool check() {
    int n = abc*e;
    int n1 = abc*d;
    if (n > 999 || n < 100)return false;
    if (n1 > 999 || n1 < 100)return false;
    if ((n1 * 10 + n) > 9999 || (n1 * 10 + n) < 1000)return false;
    if (oneof1(n) && oneof1(n1) && oneof1(n1 * 10 + n)) {
        return true;
    }
    return false;  // this was missing
}

大多数编译器在编写不返回所有返回路径值的函数时都会发出警告。 请将警告至少设置为提供此信息的级别(或者,如果这样做,请阅读编译器给您的警告)。

相关文章: