给定数字N。任务是查找第一个N Iccanobif Numbers 。
Iccanobif编号与斐波那契编号相似。第K个Iccanobif数可以通过在反转两位数后将前两个数相加而获得。
前几个Iccanobif编号为:
0, 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, 836, …..
例子:
Input : N = 5
Output : 0 1 1 2 3
Input : N = 9
Output : 0 1 1 2 3 5 8 13 39
Explanation: Upto 8th term, adding previous two
terms is required, as there is an only single digit.
For 9th term, adding 31(reversing 8th term)
and 8 will give 39.
方法:想法是将前两个Iccanobif数设为first = 0和second = 1 。现在使用demoPointer N-2进行迭代,并且每次使用以下方法中讨论的方法找到与前两个数字相反的值:反转数字。找到两个反向数字的总和,然后分别更新第一个和第二个变量。
下面是上述方法的实现:
C++
// C++ program to find first
// N Icanobif numbers
#include
using namespace std;
// Iterative function to
// reverse digits of num
int reversDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Function to print first
// N Icanobif Numbers
void icanobifNumbers(int N)
{
// Initialize first, second numbers
int first = 0, second = 1;
if (N == 1)
cout << first;
else if (N == 2)
cout << first << " " << second;
else {
// Print first two numbers
cout << first << " " << second << " ";
for (int i = 3; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
int x = reversDigits(first);
int y = reversDigits(second);
cout << x + y << " ";
int temp = second;
second = x + y;
first = temp;
}
}
}
// Driver Code
int main()
{
int N = 12;
icanobifNumbers(N);
return 0;
}
Java
// Java program to find first
// N Icanobif numbers
public class GFG{
// Iterative function to
// reverse digits of num
static int reversDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Function to print first
// N Icanobif Numbers
static void icanobifNumbers(int N)
{
// Initialize first, second numbers
int first = 0, second = 1;
if (N == 1)
System.out.print(first);
else if (N == 2)
System.out.print(first + " " + second);
else {
// Print first two numbers
System.out.print(first + " " + second + " ");
for (int i = 3; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
int x = reversDigits(first);
int y = reversDigits(second);
System.out.print(x + y + " ");
int temp = second;
second = x + y;
first = temp;
}
}
}
// Driver Code
public static void main(String []args){
int N = 12;
icanobifNumbers(N);
}
// This code is contributed by ANKITRAI1
}
Python3
# Python 3 program to find first
# N Icanobif numbers
# Iterative function to
# reverse digits of num
def reversedigit(num):
rev_num = 0
while num > 0:
rev_num = rev_num * 10 + num % 10
num = num // 10
return rev_num
# Function to print first
# N Icanobif Numbers
def icanobifNumbers(N):
# Initialize first, second numbers
first = 0
second = 1
if N == 1:
print(first)
elif N == 2:
print(first, second)
else:
# Print first two numbers
print(first, second, end = " ")
for i in range(3, N + 1):
# Reversing digit of previous
# two terms and adding them
x = reversedigit(first)
y = reversedigit(second)
print(x + y, end = " ")
temp = second
second = x + y
first = temp
# Driver code
N = 12
icanobifNumbers(N)
# This code is contributed by Shrikant13
C#
// C# program to find first
// N Icanobif numbers
using System;
public class GFG{
// Iterative function to
// reverse digits of num
static int reversDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Function to print first
// N Icanobif Numbers
static void icanobifNumbers(int N)
{
// Initialize first, second numbers
int first = 0, second = 1;
if (N == 1)
Console.Write(first);
else if (N == 2)
Console.Write(first + " " + second);
else {
// Print first two numbers
Console.Write(first + " " + second + " ");
for (int i = 3; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
int x = reversDigits(first);
int y = reversDigits(second);
Console.Write(x + y + " ");
int temp = second;
second = x + y;
first = temp;
}
}
}
// Driver Code
public static void Main(){
int N = 12;
icanobifNumbers(N);
}
}
PHP
0)
{
$rev_num = ($rev_num * 10) +
($num % 10);
$num = (int)( $num / 10);
}
return $rev_num;
}
// Function to print first
// N Icanobif Numbers
function icanobifNumbers($N)
{
// Initialize first, second numbers
$first = 0;
$second = 1;
if ($N == 1)
echo $first;
else if ($N == 2)
echo $first , " ", $second;
else
{
// Print first two numbers
echo $first, " " , $second, " ";
for ($i = 3; $i <= $N; $i++)
{
// Reversing digit of previous
// two terms and adding them
$x = reversDigits($first);
$y = reversDigits($second);
echo ($x + $y), " ";
$temp = $second;
$second = $x + $y;
$first = $temp;
}
}
}
// Driver Code
$N = 12;
icanobifNumbers($N);
// This code is ccontributed by Tushil.
?>
输出:
0 1 1 2 3 5 8 13 39 124 514 836
注意:对于较大的N值,请使用数字作为字符串。