原文:
给定一个整数 n ,任务是打印的第一个 n 术语。
示例:
输入: n = 5 输出: 1,3,4,6,8
输入: n = 10 输出: 1、3、4、6、8、9、11、12、14、16
方法:下 wythoff 序列是这样一个序列,其ntht5】项为 a(n) =地板(n φ),其中φ=(1 sqrt(5))/2。所以,我们运行一个循环,找到序列的第一个 n 项。*
下面是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
// function to print the first n terms
// of the lower wythoff sequence
void lowerwythoff(int n)
{
// calculate value of phi
double phi = (1 sqrt(5)) / 2.0;
// find the numbers
for (int i = 1; i <= n; i ) {
// a(n) = floor(n * phi)
double ans = floor(i * phi);
// print the nth numbers
cout << ans;
if (i != n)
cout << ", ";
}
}
// driver code
int main()
{
int n = 5;
lowerwythoff(n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the approach
class gfg
{
// function to print the first n terms
// of the lower wythoff sequence
static void lowerwythoff(int n)
{
// calculate value of phi
double phi = (1 math.sqrt(5)) / 2.0;
// find the numbers
for (int i = 1; i <= n; i )
{
// a(n) = floor(n * phi)
double ans = math.floor(i * phi);
// print the nth numbers
system.out.print((int)ans);
if (i != n)
system.out.print(" , ");
}
}
// driver code
public static void main(string[] args)
{
int n = 5;
lowerwythoff(n);
}
}
// this code is contributed by 29ajaykumar
python 3
# python3 implementation of the approach
# from math import sqrt,floor
from math import sqrt, floor
# function to print the first n terms
# of the lower wythoff sequence
def lowerwythoff(n) :
# calculate value of phi
phi = (1 sqrt(5)) / 2;
# find the numbers
for i in range(1, n 1) :
# a(n) = floor(n * phi)
ans = floor(i * phi);
# print the nth numbers
print(ans,end="");
if (i != n) :
print( ", ",end = "");
# driver code
if __name__ == "__main__" :
n = 5;
lowerwythoff(n);
# this code is contributed by ankitrai01
c
// c# implementation of the approach
using system;
class gfg
{
// function to print the first n terms
// of the lower wythoff sequence
static void lowerwythoff(int n)
{
// calculate value of phi
double phi = (1 math.sqrt(5)) / 2.0;
// find the numbers
for (int i = 1; i <= n; i )
{
// a(n) = floor(n * phi)
double ans = math.floor(i * phi);
// print the nth numbers
console.write((int)ans);
if (i != n)
console.write(" , ");
}
}
// driver code
static public void main ()
{
int n = 5;
lowerwythoff(n);
}
}
// this code is contributed by ajit.
java 描述语言
output:
1, 3, 4, 6, 8
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处