【问题标题】:Infinite House of Pancakes [closed]无限煎饼屋[关闭]
【发布时间】: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++


【解决方案1】:

这在划分为非等效堆栈更有效的情况下会中断。比如案例

1
9

一个人有 9 个煎饼。尽可能均匀地分成 2 组给出解决方案:

第 1 分钟:9
分钟 2:5 4
分钟 3:4 3
4分钟:3 2
5分钟:2 1
6分钟:1 0
7分钟:0 0

不均匀的分割给出了解决方案

第 1 分钟:9
分钟 2:3 6
分钟 3:3 3 3
4分钟:2 2 2
5分钟:1 1 1
6分钟:0 0 0

效率更高。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多