原文:
给定三个正整数 n,s 和 k 。任务是打印长度为 s 的所有可能序列,从 n 开始,连续元素之间的绝对差值小于 k。 示例:
input : n = 5, s = 3, k = 2
output :
5 5 5
5 5 6
5 5 4
5 6 6
5 6 7
5 6 5
5 4 4
5 4 5
5 4 3
input : n = 3, s = 2, k = 1
output :
3 3
观察,为了得到小于 k 的连续元素之间的绝对差,我们可以从 0 增加到 k–1。同样,我们可以将下一个元素从 1 减少到 k–1。 现在,为了形成所需的序列,我们首先将‘n’推送到向量。然后尝试通过对序列中的每个元素进行递归调用来填充序列的另一个元素。每次递归调用时,我们运行一个从 0 到 k–1 的循环,并将(n i)添加到序列中。一旦我们制作了大小为 s 的序列,我们将打印整个序列并返回递归调用函数并移除(n i)。 类似地,我们可以运行从 1 到 k–1 的循环,并插入(n–i)到下一个元素位置。 为了检查所需剩余元素的数量,我们将把 size–1 传递给递归调用,当 size 变为 0 时,我们将打印整个序列。 以下是本办法的实施:
c
// cpp program all sequence of length s
// starting with n such that difference
// between consecutive element is less than k.
#include
using namespace std;
// recursive function to print all sequence
// of length s starting with n such that
// difference between consecutive element
// is less than k.
void printsequence(vector& v, int n,
int s, int k)
{
// if size become 0, print the sequence.
if (s == 0) {
for (int i = 0; i < v.size(); i )
cout << v[i] << " ";
cout << endl;
return;
}
// increment the next element and make
// recursive call after inserting the
// (n i) to the sequence.
for (int i = 0; i < k; i ) {
v.push_back(n i);
printsequence(v, n i, s - 1, k);
v.pop_back();
}
// decrementing the next element and'
// make recursive call after inserting
// the (n - i) to the sequence.
for (int i = 1; i < k; i ) {
v.push_back(n - i);
printsequence(v, n - i, s - 1, k);
v.pop_back();
}
}
// wrapper function
void wrapper(int n, int s, int k)
{
vector v;
v.push_back(n);
printsequence(v, n, s - 1, k);
}
// driven program
int main()
{
int n = 5, s = 3, k = 2;
wrapper(n, s, k);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program all sequence of length s
// starting with n such that difference
// between consecutive element is less than k.
import java.io.*;
import java.util.*;
public class gfg {
static list v = new arraylist();
// recursive function to print all sequence
// of length s starting with n such that
// difference between consecutive element
// is less than k.
static void printsequence(int n,
int s, int k)
{
// if size become 0, print the sequence.
if (s == 0) {
for (int i = 0; i < v.size(); i )
system.out.print(v.get(i) " ");
system.out.println();
return;
}
// increment the next element and make
// recursive call after inserting the
// (n i) to the sequence.
for (int i = 0; i < k; i ) {
v.add(n i);
printsequence(n i, s - 1, k);
v.remove(v.size() - 1);
}
// decrementing the next element and'
// make recursive call after inserting
// the (n - i) to the sequence.
for (int i = 1; i < k; i ) {
v.add(n - i);
printsequence(n - i, s - 1, k);
v.remove(v.size() - 1);
}
}
// wrapper function
static void wrapper(int n, int s, int k)
{
v.add(n);
printsequence(n, s - 1, k);
}
// driven program
public static void main(string args[])
{
int n = 5, s = 3, k = 2;
wrapper(n, s, k);
}
}
// this code is contributed by manish shaw
// (manishshaw1)
python 3
# python3 program all sequence of length s
# starting with n such that difference
# between consecutive element is less than k.
# recursive function to print all sequence
# of length s starting with n such that
# difference between consecutive element
# is less than k.
def printsequence(v, n, s, k):
# if size become 0, print the sequence.
if (s == 0) :
for i in range(0, len(v)):
print ("{} ".format(v[i]), end="")
print ("")
return;
# increment the next element and make
# recursive call after inserting the
# (n i) to the sequence.
for i in range(0,k):
v.append(n i)
printsequence(v, n i, s - 1, k)
v.pop()
# decrementing the next element and'
# make recursive call after inserting
# the (n - i) to the sequence.
for i in range(1,k):
v.append(n - i)
printsequence(v, n - i, s - 1, k)
v.pop()
# wrapper function
def wrapper(n, s, k):
v = []
v.append(n)
printsequence(v, n, s - 1, k)
# driven program
n = 5; s = 3; k = 2;
wrapper(n, s, k);
# this code is contributed by
# manish shaw(manishshaw1)
c
// c# program all sequence of length s
// starting with n such that difference
// between consecutive element is less than k.
using system;
using system.collections.generic;
using system.linq;
using system.collections;
class gfg {
// recursive function to print all sequence
// of length s starting with n such that
// difference between consecutive element
// is less than k.
static void printsequence(ref list v, int n,
int s, int k)
{
// if size become 0, print the sequence.
if (s == 0) {
for (int i = 0; i < v.count; i )
console.write(v[i] " ");
console.writeline();
return;
}
// increment the next element and make
// recursive call after inserting the
// (n i) to the sequence.
for (int i = 0; i < k; i ) {
v.add(n i);
printsequence(ref v, n i, s - 1, k);
v.removeat(v.count - 1);
}
// decrementing the next element and'
// make recursive call after inserting
// the (n - i) to the sequence.
for (int i = 1; i < k; i ) {
v.add(n - i);
printsequence(ref v, n - i, s - 1, k);
v.removeat(v.count - 1);
}
}
// wrapper function
static void wrapper(int n, int s, int k)
{
list v = new list();
v.add(n);
printsequence(ref v, n, s - 1, k);
}
// driven program
public static void main()
{
int n = 5, s = 3, k = 2;
wrapper(n, s, k);
}
}
// this code is contributed by manish shaw
// (manishshaw1)
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
输出:
5 5 5
5 5 6
5 5 4
5 6 6
5 6 7
5 6 5
5 4 4
5 4 5
5 4 3
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处