为什么USACO分级师不接受我的"Prime Cryptarithm"解决方案?(我的电脑和在线评分机之间的输出差异)
Why does the USACO grader not accept my solution to "Prime Cryptarithm"?(Output difference between my computer and the online grader)
我正在编写解决问题的解决方案 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;
}
在 oneof1
和 check
函数中,不会为函数中的所有返回路径返回值。 因此,您的程序表现出未定义的行为,因为从返回值的函数不返回任何内容是 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
}
大多数编译器在编写不返回所有返回路径值的函数时都会发出警告。 请将警告至少设置为提供此信息的级别(或者,如果这样做,请阅读编译器给您的警告)。
相关文章:
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 如果我的容器位于两个现有值之间,那么伪造迭代器类别是否合理
- 为什么我的 cout 一次执行,尽管 cin 应该在两者之间读取?
- 这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)
- 我如何确保我的随机数在兰德生成的0和1之间不是0
- 我的编译行和我的生成文件之间的区别可能导致错误
- 如何避免int(xxx)_t之间的重叠,并使我的代码正确
- 为什么我的数据无法在信号/插槽的表格之间传输
- STLPORT.5.2.dll 和视觉C++之间有什么关系?我是否需要 stlport 才能运行我的程序
- 为什么我的shell只检测0-255之间的返回代码
- 告诉我两者之间的区别.它是在树上插入的C 代码
- 在C和我的语言之间创建FFI
- 在我的c++代码之间向matlab传递一个字符串变量
- 我的公司做得对吗,在前任之间共享数据
- 为什么我的程序是,数组中任意两个元素之间的最小差异,给出相反的结果
- 如何让我的程序分开而不是将输入之间的空格计算为整数
- 我的代码试图找到 0 - n 个数字之间的所有素数,语句"bool prime[n + 1];"中的'+ 1'有什么用?
- 为什么我的代码要求"请输入 2-12 之间的数字:"两次?
- 为什么我的静态方法的返回值与定义的构造函数(在 c++ 中)之间存在类型不匹配?