我正在研究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;
}
答案 0 :(得分:0)
两台计算机是否都是相同的操作系统?如果是这样,它们应该没问题,但是否则你需要在两个系统上进行编译,这意味着每个系统都有一个可执行文件。无论如何,当我在mac上写东西并希望在mac和linux上运行它时,无论如何都可以。