【发布时间】:2014-09-22 20:18:43
【问题描述】:
所以我正在做一个家庭作业,我需要为分区、斯特林数(第一类和第二类)和第一类切比雪夫多项式创建递归函数。我的程序应该能够让用户输入一个正整数 n,然后创建名为 Partitions.txt、Stirling1.txt、Stirling2.txt 和 Chebyshev.txt 的文件,从而创建一个包含所有值 f(k,m) 的表对于 1
#include <iostream>
#include <vector>
#include "OutputFile.h"
using namespace std;
using namespace OutputStream;
int firstKindStirling();
vector<vector<int> > getPartitions(int number, int maxElement);
int main() {
cout << "Welcome! Please input a number m:";
int m;
cin>>m;
OFile fout("Partitions.txt");
return 0;
}
vector<vector<int> > getPartitions(int number, int maxElement)
{
if (number < 1)
return vector<vector<int>>();
vector<vector<int>> partitions;
if (number <= maxElement)
partitions.push_back(number); //for some reason this doesn't want to work. Not sure what I'm missing here.
for (int i = number - maxElement; i < number; ++i)
{
// (recursively) get the partitions of `i`, with elements no larger than `n - i`
auto partitionsForI = getPartitions(i, number - i);
// add `n - i` to the front of all of those partitions
for(vector<int>& partition : partitionsForI)
{
partition.insert(partition.begin(), number - i);
}
// add these new partitions to our list.
partitions.insert(partitions.end(), partitionsForI.begin(), partitionsForI.end());
}
return partitions;
}
int firstKindStirling(int n, int k)
{
if (n == 0 && k == 0) return 1;
else if (n == 0 || k == 0) return 0;
else return -(n-1) * firstKindStirling(n-1, k) + firstKindStirling(n-1, k-1);
}
这是我的输出 .h 文件
#ifndef OUTPUT_H
#define OUTPUT_H
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sys/stat.h>
#include <sstream>
#include <memory>
namespace OutputStream {
class OFile {
std::ofstream file;
public:
OFile(std::string filename, size_t output_precision = 10) {
file.open(filename);
if(file.fail()) throw std::runtime_error("Error: cannot open file");
file.precision(output_precision);
};
/*
OFile& operator<<(int x) {
file<<x;
return *this;
}
*/
/*
OFile& operator<<(const Point2D& p) {
file<<p;
return *this;
}
*/
OFile& operator<<(const std::vector<int>& v) {
for(auto x : v) file<<x<<std::endl;
return *this;
}
template<typename T>
OFile& operator<<(const T& p) {
file << p;
return *this;
}
~OFile() { file.close(); };
};
// Strongly enumerate type
enum class FileType { Input, Output, SafeOutput };
// Partial Template Specialization
template<FileType> class File;
template<>
class File < FileType::Input > {
public:
File( const std::string& filename ) : fin(filename) {
if(fin.fail()) throw std::runtime_error("Error opening file: "+filename);
};
/** ...
IFile& allows for syntax like
fin>>a>>b>>c;
*/
File& operator>>(int& a) {
fin>>a;
return *this;
}
/**...*/
operator bool() {
return !(fin.fail());
}
operator std::string() {
return "Active";
}
// operator [data type]() {
// code here
// return [object of type data type];
// }
friend File& getline( File& fin, std::string& line) {
getline( fin.fin, line);
return fin;
}
friend File& getrow( File& fin, std::vector<int>& rows);
friend File& getmatrix( File& fin, std::vector< std::vector<int> >& table);
~File() { fin.close(); };
private:
std::ifstream fin;
};
template<>
class File < FileType::Output > {
std::ofstream file;
public:
File(std::string filename, size_t output_precision = 10) {
file.open(filename);
if(file.fail()) throw std::runtime_error("Error: cannot open file");
file.precision(output_precision);
};
/*
OFile& operator<<(int x) {
file<<x;
return *this;
}
*/
/*
OFile& operator<<(const Point2D& p) {
file<<p;
return *this;
}
*/
File& operator<<(const std::vector<int>& v) {
for(auto x : v) file<<x<<std::endl;
return *this;
}
template<typename T>
File& operator<<(const T& p) {
file << p;
return *this;
}
~File() { file.close(); };
};
}
#endif
【问题讨论】:
-
您遇到了哪些问题?你了解Stirling numbers和Chebyshev polynomials的概念吗?您使用什么语言? “分区的递归函数”是什么意思?
-
所以分区是指整数分区。因此,例如,如果我有一个整数 n,比如说 5。分区是总和为 5 的正整数的集合。{5}、{4,1}、{3,2}、{3,1、 1}、{2,2,1}、{2,1,1,1} 和 {1,1,1,1,1}。而且我不理解斯特林数或切比雪夫多项式的概念,但我的教授说我们不应该为了完成作业而需要。
-
他有没有给你关于如何生成它们的说明,然后,因为如果没有,你将必须至少有一些了解......
标签: c++ recursion partitioning partition