Print all jumping numbers in ascending order by given limits
Here given code implementation process.
/*
Java program
Print all jumping numbers in ascending order by given limits
*/
class QNode
{
public int data;
public QNode next;
public QNode(int data)
{
this.data = data;
this.next = null;
}
}
// Define custom queue class
class MyQueue
{
public QNode front;
public QNode rear;
public int size;
public MyQueue()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
public void enqueue(int data)
{
QNode node = new QNode(data);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size++;
this.rear = node;
}
// Delete front node of queue
public void dequeue()
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size--;
}
}
public int isSize()
{
return this.size;
}
public boolean isEmpty()
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
public QNode peek()
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front;
}
}
}
public class Jumping
{
public void JumpingSteps(int num)
{
if (num < 0)
{
return;
}
int v = 0;
if (num <= 10)
{
while (v <= num)
{
System.out.print(" " + v);
v++;
}
return;
}
MyQueue record = new MyQueue();
int count = 1;
int digit = 0;
System.out.print(" 0");
while (count <= num || record.isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
System.out.print(" " + count);
record.enqueue(count);
count++;
}
else
{
// Get head value
v = record.peek().data;
// Remove head node
record.dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v * 10) + (digit - 1);
if (count <= num)
{
System.out.print(" " + count);
record.enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v * 10) + (digit + 1);
if (count <= num)
{
System.out.print(" " + count);
record.enqueue(count);
}
}
}
}
else
{
record.dequeue();
}
}
}
public static void main(String[] args)
{
Jumping task = new Jumping();
// 0..1400
task.JumpingSteps(1400);
}
}
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Print all jumping numbers in ascending order by given limits
*/
class QNode
{
public: int data;
QNode *next;
QNode(int data)
{
this->data = data;
this->next = NULL;
}
};
// Define custom queue class
class MyQueue
{
public: QNode *front;
QNode *rear;
int size;
MyQueue()
{
this->front = NULL;
this->rear = NULL;
this->size = 0;
}
// Add a new node at last of queue
void enqueue(int data)
{
QNode *node = new QNode(data);
if (this->front == NULL)
{
// When first node of queue
this->front = node;
}
else
{
// Add node at last level
this->rear->next = node;
}
this->size++;
this->rear = node;
}
// Delete front node of queue
void dequeue()
{
if (this->front != NULL)
{
if (this->rear == this->front)
{
this->rear = NULL;
this->front = NULL;
}
else
{
this->front = this->front->next;
}
this->size--;
}
}
int isSize()
{
return this->size;
}
bool isEmpty()
{
if (this->isSize() == 0)
{
return true;
}
return false;
}
QNode *peek()
{
if (this->isSize() == 0)
{
return NULL;
}
else
{
return this->front;
}
}
};
class Jumping
{
public: void JumpingSteps(int num)
{
if (num < 0)
{
return;
}
int v = 0;
if (num <= 10)
{
while (v <= num)
{
cout << " " << v;
v++;
}
return;
}
MyQueue *record = new MyQueue();
int count = 1;
int digit = 0;
cout << " 0";
while (count <= num || record->isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
cout << " " << count;
record->enqueue(count);
count++;
}
else
{
// Get head value
v = record->peek()->data;
// Remove head node
record->dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v *10) + (digit - 1);
if (count <= num)
{
cout << " " << count;
record->enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v *10) + (digit + 1);
if (count <= num)
{
cout << " " << count;
record->enqueue(count);
}
}
}
}
else
{
record->dequeue();
}
}
}
};
int main()
{
Jumping *task = new Jumping();
// 0..1400
task->JumpingSteps(1400);
return 0;
}
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
// Include namespace system
using System;
/*
Csharp program
Print all jumping numbers in ascending order by given limits
*/
public class QNode
{
public int data;
public QNode next;
public QNode(int data)
{
this.data = data;
this.next = null;
}
}
// Define custom queue class
public class MyQueue
{
public QNode front;
public QNode rear;
public int size;
public MyQueue()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
public void enqueue(int data)
{
QNode node = new QNode(data);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size++;
this.rear = node;
}
// Delete front node of queue
public void dequeue()
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size--;
}
}
public int isSize()
{
return this.size;
}
public Boolean isEmpty()
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
public QNode peek()
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front;
}
}
}
public class Jumping
{
public void JumpingSteps(int num)
{
if (num < 0)
{
return;
}
int v = 0;
if (num <= 10)
{
while (v <= num)
{
Console.Write(" " + v);
v++;
}
return;
}
MyQueue record = new MyQueue();
int count = 1;
int digit = 0;
Console.Write(" 0");
while (count <= num || record.isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
Console.Write(" " + count);
record.enqueue(count);
count++;
}
else
{
// Get head value
v = record.peek().data;
// Remove head node
record.dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v * 10) + (digit - 1);
if (count <= num)
{
Console.Write(" " + count);
record.enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v * 10) + (digit + 1);
if (count <= num)
{
Console.Write(" " + count);
record.enqueue(count);
}
}
}
}
else
{
record.dequeue();
}
}
}
public static void Main(String[] args)
{
Jumping task = new Jumping();
// 0..1400
task.JumpingSteps(1400);
}
}
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
package main
import "fmt"
/*
Go program
Print all jumping numbers in ascending order by given limits
*/
type QNode struct {
data int
next * QNode
}
func getQNode(data int) * QNode {
var me *QNode = &QNode {}
me.data = data
me.next = nil
return me
}
// Define custom queue class
type MyQueue struct {
front * QNode
rear * QNode
size int
}
func getMyQueue() * MyQueue {
var me *MyQueue = &MyQueue {}
me.front = nil
me.rear = nil
me.size = 0
return me
}
// Add a new node at last of queue
func(this *MyQueue) enqueue(data int) {
var node * QNode = getQNode(data)
if this.front == nil {
// When first node of queue
this.front = node
} else {
// Add node at last level
this.rear.next = node
}
this.size++
this.rear = node
}
// Delete front node of queue
func(this *MyQueue) dequeue() {
if this.front != nil {
if this.rear == this.front {
this.rear = nil
this.front = nil
} else {
this.front = this.front.next
}
this.size--
}
}
func(this MyQueue) isSize() int {
return this.size
}
func(this MyQueue) isEmpty() bool {
if this.isSize() == 0 {
return true
}
return false
}
func(this MyQueue) peek() * QNode {
if this.isSize() == 0 {
return nil
} else {
return this.front
}
}
type Jumping struct {}
func getJumping() * Jumping {
var me *Jumping = &Jumping {}
return me
}
func(this Jumping) JumpingSteps(num int) {
if num < 0 {
return
}
var v int = 0
if num <= 10 {
for (v <= num) {
fmt.Print(" ", v)
v++
}
return
}
var record * MyQueue = getMyQueue()
var count int = 1
var digit int = 0
fmt.Print(" 0")
for (count <= num || record.isEmpty() == false) {
if count <= num {
if count <= 9 {
fmt.Print(" ", count)
record.enqueue(count)
count++
} else {
// Get head value
v = record.peek().data
// Remove head node
record.dequeue()
// Find last digit
digit = v % 10
if digit != 0 {
// Make new number
count = (v * 10) + (digit - 1)
if count <= num {
fmt.Print(" ", count)
record.enqueue(count)
}
}
if digit != 9 && count <= num {
// Make new number
count = (v * 10) + (digit + 1)
if count <= num {
fmt.Print(" ", count)
record.enqueue(count)
}
}
}
} else {
record.dequeue()
}
}
}
func main() {
var task * Jumping = getJumping()
// 0..1400
task.JumpingSteps(1400)
}
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
<?php
/*
Php program
Print all jumping numbers in ascending order by given limits
*/
class QNode
{
public $data;
public $next;
public function __construct($data)
{
$this->data = $data;
$this->next = NULL;
}
}
// Define custom queue class
class MyQueue
{
public $front;
public $rear;
public $size;
public function __construct()
{
$this->front = NULL;
$this->rear = NULL;
$this->size = 0;
}
// Add a new node at last of queue
public function enqueue($data)
{
$node = new QNode($data);
if ($this->front == NULL)
{
// When first node of queue
$this->front = $node;
}
else
{
// Add node at last level
$this->rear->next = $node;
}
$this->size++;
$this->rear = $node;
}
// Delete front node of queue
public function dequeue()
{
if ($this->front != NULL)
{
if ($this->rear == $this->front)
{
$this->rear = NULL;
$this->front = NULL;
}
else
{
$this->front = $this->front->next;
}
$this->size--;
}
}
public function isSize()
{
return $this->size;
}
public function isEmpty()
{
if ($this->isSize() == 0)
{
return true;
}
return false;
}
public function peek()
{
if ($this->isSize() == 0)
{
return NULL;
}
else
{
return $this->front;
}
}
}
class Jumping
{
public function JumpingSteps($num)
{
if ($num < 0)
{
return;
}
$v = 0;
if ($num <= 10)
{
while ($v <= $num)
{
echo(" ".$v);
$v++;
}
return;
}
$record = new MyQueue();
$count = 1;
$digit = 0;
echo(" 0");
while ($count <= $num || $record->isEmpty() == false)
{
if ($count <= $num)
{
if ($count <= 9)
{
echo(" ".$count);
$record->enqueue($count);
$count++;
}
else
{
// Get head value
$v = $record->peek()->data;
// Remove head node
$record->dequeue();
// Find last digit
$digit = $v % 10;
if ($digit != 0)
{
// Make new number
$count = ($v * 10) + ($digit - 1);
if ($count <= $num)
{
echo(" ".$count);
$record->enqueue($count);
}
}
if ($digit != 9 && $count <= $num)
{
// Make new number
$count = ($v * 10) + ($digit + 1);
if ($count <= $num)
{
echo(" ".$count);
$record->enqueue($count);
}
}
}
}
else
{
$record->dequeue();
}
}
}
}
function main()
{
$task = new Jumping();
// 0..1400
$task->JumpingSteps(1400);
}
main();
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
/*
Node JS program
Print all jumping numbers in ascending order by given limits
*/
class QNode
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
// Define custom queue class
class MyQueue
{
constructor()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
enqueue(data)
{
var node = new QNode(data);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size++;
this.rear = node;
}
// Delete front node of queue
dequeue()
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size--;
}
}
isSize()
{
return this.size;
}
isEmpty()
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
peek()
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front;
}
}
}
class Jumping
{
JumpingSteps(num)
{
if (num < 0)
{
return;
}
var v = 0;
if (num <= 10)
{
while (v <= num)
{
process.stdout.write(" " + v);
v++;
}
return;
}
var record = new MyQueue();
var count = 1;
var digit = 0;
process.stdout.write(" 0");
while (count <= num || record.isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
process.stdout.write(" " + count);
record.enqueue(count);
count++;
}
else
{
// Get head value
v = record.peek().data;
// Remove head node
record.dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v * 10) + (digit - 1);
if (count <= num)
{
process.stdout.write(" " + count);
record.enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v * 10) + (digit + 1);
if (count <= num)
{
process.stdout.write(" " + count);
record.enqueue(count);
}
}
}
}
else
{
record.dequeue();
}
}
}
}
function main()
{
var task = new Jumping();
// 0..1400
task.JumpingSteps(1400);
}
main();
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
# Python 3 program
# Print all jumping numbers in ascending order by given limits
class QNode :
def __init__(self, data) :
self.data = data
self.next = None
# Define custom queue class
class MyQueue :
def __init__(self) :
self.front = None
self.rear = None
self.size = 0
# Add a new node at last of queue
def enqueue(self, data) :
node = QNode(data)
if (self.front == None) :
# When first node of queue
self.front = node
else :
# Add node at last level
self.rear.next = node
self.size += 1
self.rear = node
# Delete front node of queue
def dequeue(self) :
if (self.front != None) :
if (self.rear == self.front) :
self.rear = None
self.front = None
else :
self.front = self.front.next
self.size -= 1
def isSize(self) :
return self.size
def isEmpty(self) :
if (self.isSize() == 0) :
return True
return False
def peek(self) :
if (self.isSize() == 0) :
return None
else :
return self.front
class Jumping :
def JumpingSteps(self, num) :
if (num < 0) :
return
v = 0
if (num <= 10) :
while (v <= num) :
print(" ", v, end = "")
v += 1
return
record = MyQueue()
count = 1
digit = 0
print(" 0", end = "")
while (count <= num or record.isEmpty() == False) :
if (count <= num) :
if (count <= 9) :
print(" ", count, end = "")
record.enqueue(count)
count += 1
else :
# Get head value
v = record.peek().data
# Remove head node
record.dequeue()
# Find last digit
digit = v % 10
if (digit != 0) :
# Make new number
count = (v * 10) + (digit - 1)
if (count <= num) :
print(" ", count, end = "")
record.enqueue(count)
if (digit != 9 and count <= num) :
# Make new number
count = (v * 10) + (digit + 1)
if (count <= num) :
print(" ", count, end = "")
record.enqueue(count)
else :
record.dequeue()
def main() :
task = Jumping()
# 0..1400
task.JumpingSteps(1400)
if __name__ == "__main__": main()
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
# Ruby program
# Print all jumping numbers in ascending order by given limits
class QNode
# Define the accessor and reader of class QNode
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end
end
# Define custom queue class
class MyQueue
# Define the accessor and reader of class MyQueue
attr_reader :front, :rear, :size
attr_accessor :front, :rear, :size
def initialize()
self.front = nil
self.rear = nil
self.size = 0
end
# Add a new node at last of queue
def enqueue(data)
node = QNode.new(data)
if (self.front == nil)
# When first node of queue
self.front = node
else
# Add node at last level
self.rear.next = node
end
self.size += 1
self.rear = node
end
# Delete front node of queue
def dequeue()
if (self.front != nil)
if (self.rear == self.front)
self.rear = nil
self.front = nil
else
self.front = self.front.next
end
self.size -= 1
end
end
def isSize()
return self.size
end
def isEmpty()
if (self.isSize() == 0)
return true
end
return false
end
def peek()
if (self.isSize() == 0)
return nil
else
return self.front
end
end
end
class Jumping
def JumpingSteps(num)
if (num < 0)
return
end
v = 0
if (num <= 10)
while (v <= num)
print(" ", v)
v += 1
end
return
end
record = MyQueue.new()
count = 1
digit = 0
print(" 0")
while (count <= num || record.isEmpty() == false)
if (count <= num)
if (count <= 9)
print(" ", count)
record.enqueue(count)
count += 1
else
# Get head value
v = record.peek().data
# Remove head node
record.dequeue()
# Find last digit
digit = v % 10
if (digit != 0)
# Make new number
count = (v * 10) + (digit - 1)
if (count <= num)
print(" ", count)
record.enqueue(count)
end
end
if (digit != 9 && count <= num)
# Make new number
count = (v * 10) + (digit + 1)
if (count <= num)
print(" ", count)
record.enqueue(count)
end
end
end
else
record.dequeue()
end
end
end
end
def main()
task = Jumping.new()
# 0..1400
task.JumpingSteps(1400)
end
main()
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
/*
Scala program
Print all jumping numbers in ascending order by given limits
*/
class QNode(var data: Int,
var next: QNode)
{
def this(data: Int)
{
this(data, null);
}
}
// Define custom queue class
class MyQueue(var front: QNode,
var rear: QNode,
var size: Int)
{
def this()
{
this(null, null, 0);
}
// Add a new node at last of queue
def enqueue(data: Int): Unit = {
var node: QNode = new QNode(data);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size += 1;
this.rear = node;
}
// Delete front node of queue
def dequeue(): Unit = {
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size -= 1;
}
}
def isSize(): Int = {
return this.size;
}
def isEmpty(): Boolean = {
if (this.isSize() == 0)
{
return true;
}
return false;
}
def peek(): QNode = {
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front;
}
}
}
class Jumping()
{
def JumpingSteps(num: Int): Unit = {
if (num < 0)
{
return;
}
var v: Int = 0;
if (num <= 10)
{
while (v <= num)
{
print(" " + v);
v += 1;
}
return;
}
var record: MyQueue = new MyQueue();
var count: Int = 1;
var digit: Int = 0;
print(" 0");
while (count <= num || record.isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
print(" " + count);
record.enqueue(count);
count += 1;
}
else
{
// Get head value
v = record.peek().data;
// Remove head node
record.dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v * 10) + (digit - 1);
if (count <= num)
{
print(" " + count);
record.enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v * 10) + (digit + 1);
if (count <= num)
{
print(" " + count);
record.enqueue(count);
}
}
}
}
else
{
record.dequeue();
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Jumping = new Jumping();
// 0..1400
task.JumpingSteps(1400);
}
}
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
/*
Swift 4 program
Print all jumping numbers in ascending order by given limits
*/
class QNode
{
var data: Int;
var next: QNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
// Define custom queue class
class MyQueue
{
var front: QNode? ;
var rear: QNode? ;
var size: Int;
init()
{
self.front = nil;
self.rear = nil;
self.size = 0;
}
// Add a new node at last of queue
func enqueue(_ data: Int)
{
let node: QNode = QNode(data);
if (self.front == nil)
{
// When first node of queue
self.front = node;
}
else
{
// Add node at last level
self.rear!.next = node;
}
self.size += 1;
self.rear = node;
}
// Delete front node of queue
func dequeue()
{
if (self.front != nil)
{
if (self.rear === self.front)
{
self.rear = nil;
self.front = nil;
}
else
{
self.front = self.front!.next;
}
self.size -= 1;
}
}
func isSize() -> Int
{
return self.size;
}
func isEmpty() -> Bool
{
if (self.isSize() == 0)
{
return true;
}
return false;
}
func peek() -> QNode?
{
if (self.isSize() == 0)
{
return nil;
}
else
{
return self.front;
}
}
}
class Jumping
{
func JumpingSteps(_ num: Int)
{
if (num < 0)
{
return;
}
var v: Int = 0;
if (num <= 10)
{
while (v <= num)
{
print(" ", v, terminator: "");
v += 1;
}
return;
}
let record: MyQueue = MyQueue();
var count: Int = 1;
var digit: Int = 0;
print(" 0", terminator: "");
while (count <= num || record.isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
print(" ", count, terminator: "");
record.enqueue(count);
count += 1;
}
else
{
// Get head value
v = record.peek()!.data;
// Remove head node
record.dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v * 10) + (digit - 1);
if (count <= num)
{
print(" ", count, terminator: "");
record.enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v * 10) + (digit + 1);
if (count <= num)
{
print(" ", count, terminator: "");
record.enqueue(count);
}
}
}
}
else
{
record.dequeue();
}
}
}
}
func main()
{
let task: Jumping = Jumping();
// 0..1400
task.JumpingSteps(1400);
}
main();
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
/*
Kotlin program
Print all jumping numbers in ascending order by given limits
*/
class QNode
{
var data: Int;
var next: QNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
// Define custom queue class
class MyQueue
{
var front: QNode ? ;
var rear: QNode ? ;
var size: Int;
constructor()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
fun enqueue(data: Int): Unit
{
val node: QNode = QNode(data);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear?.next = node;
}
this.size += 1;
this.rear = node;
}
// Delete front node of queue
fun dequeue(): Unit
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front?.next;
}
this.size -= 1;
}
}
fun isSize(): Int
{
return this.size;
}
fun isEmpty(): Boolean
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
fun peek(): QNode ?
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front;
}
}
}
class Jumping
{
fun JumpingSteps(num: Int): Unit
{
if (num < 0)
{
return;
}
var v: Int = 0;
if (num <= 10)
{
while (v <= num)
{
print(" " + v);
v += 1;
}
return;
}
val record: MyQueue = MyQueue();
var count: Int = 1;
var digit: Int ;
print(" 0");
while (count <= num || record.isEmpty() == false)
{
if (count <= num)
{
if (count <= 9)
{
print(" " + count);
record.enqueue(count);
count += 1;
}
else
{
// Get head value
v = record.peek()!!.data;
// Remove head node
record.dequeue();
// Find last digit
digit = v % 10;
if (digit != 0)
{
// Make new number
count = (v * 10) + (digit - 1);
if (count <= num)
{
print(" " + count);
record.enqueue(count);
}
}
if (digit != 9 && count <= num)
{
// Make new number
count = (v * 10) + (digit + 1);
if (count <= num)
{
print(" " + count);
record.enqueue(count);
}
}
}
}
else
{
record.dequeue();
}
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Jumping = Jumping();
// 0..1400
task.JumpingSteps(1400);
}
Output
0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment