今年我正在上高中的计算机编码课程,我正在尝试做贪婪的礼物赠送者(http://cerberus.delosent.com:791/usacoprob2?a=2uhftHpQUHa&S=gift1)。我提交了它,得到了一个执行错误,上面写着:
执行错误:您的程序没有生成被判定为正确的答案。程序以0.005秒的速度停止,它使用了3496 KB的内存。您的回答长度为119 KB;正确的长度为121。在字符21处,您的答案是'1‘,而正确的答案是’5‘。
我快完成了,它给出了四个正确的答案。我不知道怎么修好它。我的一个朋友告诉我检查我的变量,我做了,但据我所知,所有这些都是正确的。
新的守则如下:
/*
ID : aknorth1
PROB : gift1
LANG : C++
*/
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
const int ARRAY_SIZE=10;
int groupSize, numReceivers, giveAway;
int bankAcct[ARRAY_SIZE];
string giver, receivers;
string groupPeople[ARRAY_SIZE];
ofstream fout ("gift1.out");
ifstream fin ("gift1.in");
fin >> groupSize;
for (int j=0; j<groupSize; j++)
{
fin >> groupPeople[j];
bankAcct[j]=0;
}
for(int x=0; x<groupSize; x++)
{
fin >> giver;
fin >> giveAway;
fin >> numReceivers;
for (int j=0; j<numReceivers; j++)
{
if (giver == groupPeople[j])
{
bankAcct[j] -= giveAway;
if (numReceivers != 0)
{
bankAcct[j] += (giveAway % numReceivers);
}
}
}
for(int j=0; j<numReceivers; j++)
{
fin >> receivers;
for (int q=0; q<groupSize; q++)
if (groupPeople[q] == receivers)
{
if (numReceivers != 0)
{
bankAcct[q] += (giveAway / numReceivers);
}
}
}
}
for (int j=0; j<groupSize; j++)
{
fout << groupPeople[j]<< " " << bankAcct[j] << endl;
}
return 0;
}发布于 2014-12-24 20:56:47
您的代码不遵循输入格式的规范。
fin >> giver; // dave
fin >> giveAway; // gives away 200
fin >> numReceivers; // to 3 receivers
for (int j=0; j<numReceivers; j++)
{
if (giver == groupPeople[j]) // if dave was 4th in the list then what?
{
bankAcct[j] -= giveAway;
if (numReceivers != 0)
{
bankAcct[j] += (giveAway % numReceivers); // giver gives to himself?
}
}
}可能还有更多的,停在这里。
https://stackoverflow.com/questions/27641244
复制相似问题