一个统计指定递增方式的基础向量的个数问题
来源:互联网 发布:淘宝联盟鹊桥入口 编辑:程序博客网 时间:2024/06/05 16:40
问题描述:
输入一个大于等于1的整数,输出满足下面格式的基础向量:
向量的最大元素值不大于n;
各向量元素按照递增顺序排列,即靠后的任意元素均大于它的前面的元素。
基础向量是可以通过各元素均加上某一数构成满足上述方式的基元向量
问题解答:
采用在基本向量后附加一个元素,求解向量个数多一的新的基元向量。
#include <iostream>#include <vector>using namespace std;//得到p维向量的个数。该向量的元素成递增趋势,且各个向量元素值最大不超过n.//将i维的向量的总个数存储到向量numberVector的第i-1位中//lastElementsOfVector:存储某i维(i < p)向量最后一位元素unsigned long long getP_DimensionVectorNumber(const int n,const int p,vector<unsigned long long> &numberVector){if((1 > n)||(n < p)||(p < 1))return 0;numberVector.at(0) = 1; //1维向量有1个,即:{1}unsigned long long rows = n - 1;if(0 < rows){ //p维向量个数,在分配存储种子向量的矩阵时作为行数 //2维向量有n-1个,即:{1,2},{1,3},{1,4},...,{1,p} numberVector.at(1) = rows;}else//n == 1 无须继续 return numberVector.at(0);vector<int> lastElementsOfVector;int i;//初始时,存取的是二维向量的最后一位:2,3,...,nfor(i = 1;i < n;i++)lastElementsOfVector.push_back(i+1);vector<int> newVector(lastElementsOfVector);//暂存lastElementsOfVector的元素值int indexSum = 2; //已经存取了2个,因此计数从2开始 int rowNumber = 0; for(rowNumber = 0;rowNumber < n-2;rowNumber++) { lastElementsOfVector.clear(); lastElementsOfVector.reserve(n); rows = 0; unsigned long long j = 0; for(j = 0;j < newVector.size();j++) { int currentValue = newVector.at(j); if(currentValue < n) { int k = 1; //算出新的种子向量最后一位可能取得值的总数,比如 //初始种子向量为(1,2)2维向量,要生成长度为3的3维向量向量;向量最大元素值为5,则3维种子向量最后一位可以是3,4,5共3种 int maxValue = n - currentValue; rows += maxValue; for(k = 1;k <= maxValue;k++){ lastElementsOfVector.push_back(currentValue+k); } } } //统计完该维度向量的总个数,将它压入numberVector中 numberVector.at(indexSum++) = rows; //将numberVector赋值给newVector newVector.clear(); newVector.reserve(lastElementsOfVector.size()); for(j = 0;j < lastElementsOfVector.size();j++) { newVector.push_back(lastElementsOfVector.at(j)); } } return numberVector.at(p-1);}//输出向量元素void printVector(const vector<unsigned long long> &intVector){int i = 0;int len = intVector.size();for(;i < len;i++){cout<<intVector[i]<<"\t";if(0 == ((i+1)%5)) //每5个元素输出后换行cout<<endl;}}int main(){ cout << "please input the maximum value of the element:" << endl; int n = 5; cin>>n; if(1 > n) { cerr<<"n must be larger than 0.!"<<endl; return -1; } int p = 3; cout<<"please input the vector dimension:(must be smaller than n you input just now.):\n"; cin>>p; int resultOfCompute = 3; vector <unsigned long long> numberVector(n); resultOfCompute = getP_DimensionVectorNumber(n,p,numberVector); if(-1 == resultOfCompute) cerr<<"your input is illegal. please check if:(1 > n) or (n < p) or (p < 1)"<<endl; else { cout<<"total number of p dimension vector whose maximum value is :"<<resultOfCompute<<endl; cout<<"number of vectors whose dimension is between 1 and n is as follows(the first one stands for the number of 1 dimension vector,and the last one stands for the n dimension vector.):"<<endl; printVector(numberVector); } return 0;}
0 0
- 一个统计指定递增方式的基础向量的个数问题
- 关于一个统计个数的查询问题
- 统计一个含有英文单词的文本文件中的单词个数以及指定单词个数
- 统计1的个数问题
- matlab 统计向量中不同元素的个数
- 随机返回一个指定向量的垂直向量
- 统计一个文件的单词的个数
- 基础篇---统计大写字母的个数
- 统计一个子字符串的个数 Java
- 统计一个数中1的个数
- 统计一个字节中1的个数
- 统计一个文件英文单词的个数
- 统计一个句子单词的个数
- C++ 统计一个类对象的个数
- 统计一个数中1的个数
- 统计一个数阶乘0的个数
- 【一个句子里,单词的个数统计】
- 统计一个字符串中数字的个数
- poj 1287 Networking
- unity3d游戏开发经验之对于关卡类游戏的技巧
- unity3D游戏开发之浅谈如何提高游戏生命力
- EJB-MDB
- 标准的软件工程过程之文档标准
- 一个统计指定递增方式的基础向量的个数问题
- unity3f游戏开发之游戏设计中运营重用体系
- java Jstatd命令(Java Statistics Monitoring Daemon)
- unity3d游戏开发之我的游戏理念
- 蓝桥杯 回型嵌套 递归 解题报告
- JavaEE介绍
- Unity3D游戏开发之如何隐藏物体
- 高性能的Redis代理TwemProxy
- Unity3D游戏开发之格式简析