【发布时间】:2015-04-12 02:13:06
【问题描述】:
这里是相关问题的链接:https://code.google.com/codejam/contest/6224486/dashboard#s=p1
好的,所以我有点担心这个问题。现在资格赛结束了,有人知道为什么这不起作用吗?我检查了大约 50 个不同的案例,但找不到一个不起作用的案例。下面是我的完整代码。
基本上,寻找我的算法可能出错的任何情况。
//Includes
#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <stdlib.h>
using namespace std;
//Functions
int solve(list<int>);
//Main
int main() {
int minutes = 0;
int numTestCases = 0;
int initNonEmpty = 0;
char tempChar;
int tempInt;
list<int> people;
//Import Data
//Get number of Test cases
ifstream infile;
infile.clear();
infile.open("B-small-attempt5.in");
//get NumTestCases
infile >> numTestCases;
int solution[numTestCases];
//Solve it
for(int i=0; i<numTestCases; i++){
//Reset vars
initNonEmpty=0;
infile >> initNonEmpty;
people.clear();
//Input data
for(int j=0; j<initNonEmpty; j++){
infile >> tempInt;
cout << tempInt;
people.push_back(tempInt);
}
cout << endl;
//Solve the set
people.sort();
people.reverse();
tempInt = solve(people);
cout << "Solve returns: " << tempInt << endl << endl;
solution[i] = tempInt;
}
//Output
ofstream outfile;
outfile.open("RealCase6.out");
for (int i=0; i < numTestCases; i++){
cout << "Case #" << i+1 << ": " << solution[i] << endl;
outfile << "Case #" << i+1 << ": " << solution[i] << endl;
}
}
int solve( list<int> data){
cout << "Starting Solve functions." << endl;
int tempMax;
int max =data.front();
int test=0;
cout << "Max: " << max << endl;
//Test if base case
if(max<=3){
cout << "Reached base case." << endl;
return max;
}
else if (max % 2 == 0 ){
cout << "Max is even" << endl;
tempMax = max/2;
data.pop_front();
data.push_back(tempMax);
data.push_back(tempMax);
data.sort();
data.reverse();
test=solve(data);
test=test+1;
cout << "test is:" << test << endl;
cout << "max is:" << max << endl;
if( test<=max){
return test;
} else {
return max;
}
}
else {
cout << "Max is odd" << endl;
tempMax = max/2;
data.pop_front();
data.push_back(tempMax);
data.push_back(tempMax+1);
data.sort();
data.reverse();
test=solve(data);
test=test+1;
cout << "test is:" << test << endl;
cout << "max is:" << max << endl;
if(test<=max){
return test;
} else {
return max;
}
}
}
这是我针对 6 种不同情况的输入/输出。 https://www.dropbox.com/sh/55wq52lzuygd82s/AABYxJJ7zaeoMhgCmymJcwnAa?dl=0
如果现在开始询问问题还为时过早,我会删除它。抱歉,格式化了,但我正试图快速完成这项工作。
Edit1:添加了问题的链接。
Edit2:其他人发现了错误。我只想将事物分成 2 组,例如一个人有 9 个煎饼,将其分成 6 和 3 组,然后 3 3 3 效果最好。
【问题讨论】:
-
到底是什么问题?什么不起作用?您不敢相信我们能够猜出您的问题所在。
-
@vsoftco 在资格赛中提交时,我只被告知我的解决方案不正确。我希望看到它的其他人可以指出任何愚蠢的错误。据我所知,它应该可以正常工作。
-
您似乎假设我们知道“无限煎饼屋”问题是什么。一方面,我不知道。
-
@KeithThompson 抱歉,我会在描述中添加指向问题的链接。
-
除了你的答案:顶级竞争对手的答案在某些情况下给出了非最佳答案——例如,
7而不是6的情况2 8 1 8——所以我认为谷歌的评分不正确。
标签: c++