gpt4 book ai didi

c++ - 我的程序可以在我的电脑上运行,但不能在 CodeEval 上运行

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:33 25 4
gpt4 key购买 nike

我现在正在应对 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++ - 我的程序可以在我的电脑上运行,但不能在 CodeEval 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37554151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com