poj 1003 hangover 水题

来源:互联网 发布:网络带给大的好处 编辑:程序博客网 时间:2024/04/29 04:58

这道题目是我现在遇到的最简单的一题,(忽略1000),问题没有任何难度,就是寻找恰好大于问题所需值的卡片个数,我们这里第一反应是给他建立一个表格记录对应n张卡片最大长度,然后通过二分查找,很容易得到所需卡片数目。(可能是最近在看动态规划的原因吧,有些草木皆兵了)。

另外,提交后发现其他孩子的源代码,用C写的,很简洁,他们拿到这个数直接判断,和我们这个方法思路不太一样,当做一种启发吧。http://blog.csdn.net/fight_for_my_dream/article/details/38226397

Source Code

Problem: 1003 User: zhyh2010Memory: 224K Time: 0MSLanguage: C++ Result: Accepted

#include <iostream>using namespace std;const int MAX_SIZE = 276;class hangover{public:hangover();~hangover();void input();void output();void createTable();void searchTable(int i, int j, float data);void algorithm();private:float m_res[MAX_SIZE + 1];float m_search;float m_find;};hangover::hangover(){memset(m_res, 0, MAX_SIZE + 1);m_find = -1;}hangover::~hangover(){}void hangover::input(){}void hangover::output(){cout << m_find <<" card(s)"<< endl;}void hangover::createTable(){for (int i = 1; i != MAX_SIZE + 1; ++i){m_res[i] = m_res[i - 1] + 1.0 / (i + 1);}}void hangover::searchTable(int i,int j,float data){int mid = (i + j) / 2;if (i + 1 == j){m_find = j;}else if (m_res[mid] < data){searchTable(mid, j, data);}else{searchTable(i, mid, data);}}void hangover::algorithm(){createTable();while (cin >> m_search){if (m_search == 0.00){break;}searchTable(0, MAX_SIZE, m_search);output();}}int main(int argc, char ** argv){hangover instance;instance.algorithm();}


0 0
原创粉丝点击