我的程序在我的计算机上运行,但在CodeEval上不起作用
My program works on my computer, but not on CodeEval
我现在正在研究CodeEval上的一个简单挑战。您需要逐行从文件中获取输入,每行包含由管道分隔的十六进制数字和二进制数字。目标是对左侧的所有十六进制数求和,对右侧的二进制数求和,并测试哪个总和更大。如果右侧(二进制边)大于或等于十六进制边,则打印"True",如果不是,则打印"False"。示例行是"5e 7d 59 |1101100 10010101 1100111",输出将为真,因为右侧大于左侧。我的代码在我的计算机上打印了正确的输出,但在 CodeEval 上,没有结果输出,我的分数为零。未列出任何错误。有没有我看不到的问题?
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <math.h>
using namespace std;
fstream myFile;
string line;
vector<string> panNums;
int sum1, sum2;
int toDec(string s);
int main(int argc, char *argv[])
{
//open the file
// get numbers by line
myFile.open(argv[1]);
while(getline(myFile, line))
{
//cout << line << endl;
istringstream mystream(line);
string nums;
// read in each number into string nums one by one
// then add that number to the vector that was created
while(mystream)
{
mystream >> nums;
panNums.push_back(nums);
}
bool afterSep = false;
sum1 = 0;
sum2 = 0;
for(int i = 0; i < panNums.size() - 1; i++)
{
stringstream stream;
if(panNums.at(i) == "|")
{
sum1 = sum2;
sum2 = 0;
afterSep = true;
i++;
}
// if true, do stuff
if(afterSep)
{
// deals with the binary side
sum2 += toDec(panNums.at(i));
}
// if false, do other stuff
else
{
// deals with the hexidecimal side
istringstream f(panNums.at(i));
int temp;
// reading hex number into int(AKA converting to int)
f >> hex >> temp;
sum2 += temp;
}
}
// cout << "sum1 " << sum1 << endl;
// cout << "sum2 " << sum2 << endl;
if(sum2 >= sum1)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
// clear the current vector in order to exclusively have the next line of text stored
panNums.clear();
}
}
int toDec(string s)
{
int num = 0;
int i = s.size() - 1;
// starts at index 0
// which is really the 2^6 or however big the binary number is
for(int a = 0; a < s.size(); a++)
{
if(s.substr(i, 1) == "1")
{
num += pow(2, a);
}
i--;
}
// cout << num << endl;
return num;
}
两台计算机是相同的操作系统吗?如果是这样,它们应该没问题,但否则您需要在两个系统上编译,这意味着每个系统都有一个可执行文件。无论如何,当我在 mac 上编写东西并想在 mac 和 Linux 上运行它时,这在这里都有效。
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 我在使用 boost::serialization 时遇到了一个问题,我的代码在 Linux 中运行良好,但在 Wind
- 我的代码在作为参数传入 .begin() 时不起作用,但在我将 .begin() 转换为迭代器后工作
- 为什么我的代码在没有 chroot 函数的情况下工作,但使用 chroot 函数失败?
- 我的代码在Qt Creator中工作,但在Cloud9中不起作用
- 我的代码在构建时没有任何错误,但无法正常工作
- C++ 我的函数在我的 Image 类中工作,但不在主例程中工作
- 我的C 程序在CodeBlocks中提供了正确的结果,但在Visual Basic 2005 Express Edi
- 我的老师在课堂上用这个函数作为例子,但我不在场。有人可以帮助我,只是解释它的作用吗?
- 我的程序在Windows机器上崩溃了,但在Linux上运行良好
- 我的程序在我的计算机上运行,但在CodeEval上不起作用
- 为什么我的 c++ 程序在 Eclipse 中没有输出而终止,但在 Visual Studio 中工作
- 我的程序在Windows上使用MSVC和G 编译器运行,但在Linux上不运行
- 为什么我的程序在Windows上运行良好,但在linux上运行不好
- 为什么在VS 2010 C++中,我的程序在调试器中运行良好时会崩溃,但在没有调试器的情况下运行时会崩溃
- C++为什么我的代码在Ubuntu上给出了想要的结果,但在Windows7上却有未定义的行为
- c++排序数组.我的分类在第一次传球时有效,但在那之后就不行了
- 为什么我的 char* 在循环中分配了一个字符串,但之后却"(null)"?编译器错误?
- 解析字符串:我的代码在测试后显然可以工作,但不雅
- Win8如何将进程分类为"Background Process"?我的应用在 Win7 中运行,但在 Win8 中不显示任何 UI