C++ 对于n是动态的循环,我们如何进行n嵌套
我已阅读了以下问题,但尚未找到解决问题的方法:C++ 对于n是动态的循环,我们如何进行n嵌套,c++,arrays,algorithm,recursion,C++,Arrays,Algorithm,Recursion,我已阅读了以下问题,但尚未找到解决问题的方法: 实际问题陈述如下:- 乔布和吉米都在玩数字游戏。Job给Jimmy一个数字数组,让他说出长度L的非递减序列的最小可能的最后一个数字 输入格式 第一个输入行由一个数组N的大小的数字组成 下一行包含数组中N个空格分隔的元素 下一行包含非递减序列的长度,即L 输出格式 您必须打印序列的最小可能最后一个数字,如果它们不是长度L的非递减序列,则打印-1 样本输入 7 9 7 2 5 4 11 12 3 样本输出 11 解释 在样本输入中,长度L=
7
9 7 2 5 4 11 12
3
样本输出
11
解释
在样本输入中,长度L=3的可能非递减序列为(9,11,12),(7,11,12),(2,5,11),(2,4,11),(2,5,12),(2,11,12),(5,11,12),(4,11,12),序列(2,5,11)和(2,4,11)的最小最后一个数为11。因此,答案是11。”
我的代码
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int fact(int y,int x)
{
static int temp=0;
if(temp==x)
{
temp=0;
return 1;
}
else
{
++temp;
return y*fact((y-1),x);
}
}
int main() {
int num,randmax,n,s,q,w last=-1, minlast=-1;
cin>>n;
vector<int> a(n);
for(int i=0;i<n; i++)
{
cin>>a[i];
}
cin>>s;
vector<vector<int>> c;
q=fact(s);
c.resize(q);
for(int i = 0 ; i < q ; ++i)
{
//Grow Columns by n
a[i].resize(s);
}
w=q;
randmax=n-1;
int k=0;
while(w)
{
for(int i=0 ; i<n ; i++){
}
num=rand()%randmax; // this works perfect as expected
c[][i]=a[num];
}
w--;
}
/*for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n; k++)
{
if((a[i]<=a[j])&&(a[j]<=a[k]))
{
last=a[k];
if(minlast=-1)
{
minlast=a[k];
}
if(last<minlast){
minlast=last;
}
}
}
}
}
*/
cout<<last;
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
整数事实(整数y,整数x)
{
静态温度=0;
如果(温度==x)
{
温度=0;
返回1;
}
其他的
{
++温度;
返回y*事实((y-1),x);
}
}
int main(){
int num,randmax,n,s,q,w last=-1,minlast=-1;
cin>>n;
向量a(n);
对于(int i=0;i>a[i];
}
cin>>s;
载体c;
q=事实;
c、 调整大小(q);
对于(int i=0;ifor(int i=0;i正如您的链接所指出的,您可以通过使用数组来模拟动态数量的for循环,这里给出了一个很好的实现
但对于您给出的实际问题,我认为根本不需要动态的For循环数量。该问题是一个标准的非递减子序列问题,具有微小的变化
#include <iostream>
#include <fstream>
int N, L;
int arr[100], seq[100];
int bst;
int main() {
std::ifstream file ("c.txt");
file >> N;
for(int n = 0; n < N; ++n)
file >> arr[n];
file >> L;
file.close();
bst = 1e9;
for(int i = 0; i <= N; ++i) seq[i] = 1e9;
for(int i = 0; i < N; ++i)
{
int x = 0;
while(seq[x] < arr[i]) ++x;
seq[x] = arr[i];
if(x + 1 >= L)
bst = std::min(bst, arr[i]);
}
std::cout << bst << std::endl;
return 0;
}
#包括
#包括
int N,L;
整数arr[100],序号[100];
英国理工大学国际学院;
int main(){
std::ifstream文件(“c.txt”);
文件>>N;
对于(int n=0;n>arr[n];
文件>>L;
file.close();
bst=1e9;
对于(int i=0;i=L)
bst=std::min(bst,arr[i]);
}
std::cout@MikeCAT谢谢你的EDI你已经找到了你问题的很多答案,但是你要求的更多。你对其他答案没有什么了解吗?问我们一些现有答案没有涵盖的具体问题,或者这只是另一个副本,将被关闭。@user2357112我们可以不用递归来做吗?就像我发现r一样这是一场势不可挡的比赛。。。