Hi..
I a student in the Universty and my teacher gave me a crazy qeustion that I spent long time on it to find the idea which lead m to solve this problem ...
The problem is a bout " Wrap-around number" :
A wraparound number has three properties:
a. Each digit describes where the next one is located (by counting to the right
and wrapping around when needed).
b. All the digits are landed on, once each.
c. After using each digit once, you arrive back at the original (leftmost) digit.
For example, 3162 is a wraparound number as illustrated below:
Start with the leftmost digit, the 3 ................................. 3 1 6 2
Take the number 3 and count three digits to the
right, landing on 2 ..................................................... 3 1 6 2Take the number 2 and count two digits to the
right, wrapping around to the left, landing
on 1 ............................................................................ 3 1 6 2
Take the number 1 and count one digit to the
right, landing on 6 ..................................................... 3 1 6 2
Take the number 6 and count six digits to the
right, wrapping around when needed, landing
on 3 ............................................................................ 3 1 6 2
The easiest way would be to use two vectors: one of integers (or shorts) and one of booleans. To make sections of the number and to keep track of what numbers have been landed on. Then just use some integer as your step and wrap it using modulo.
Thank you dude for helping ...
I will try it now hoping thats will work ...
--------------------
I try it and I use two vectors as you said as shows to you :
1 2 3
vector<int>wrap(4);
vector<bool>vect(4,true);
int index;
but how to move based on the number where compiler stop !!?
Also what is the need of the modulos in this problem !!?
I'd recommend vectors. Modulo is required for the wrap around. Let's say we have:
3162 <-- DIGITS
0123 <-- INDICE
You'd have step 3 at the beginning. You thus go to the 3rd digit (and mark it as landed on), you then add the contents of that one to the step (you'll get 5 in the example). If you modulo 5 by 4 (the length of the number) you get: 1, which is the next piece. Etc.
You will have the easiest time if you get out a piece of paper and a pencil and draw how to do it yourself.
Here's one I can do:
┌─┬─┬─┬─┐
│ │ │ │ │ Start with some number of digits to fill. Here're four.
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│ │X│ │ │ We'll start with the second digit
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│ │2│ │X│ Moving to the fourth
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│X│2│ │5│ Moving to the fifth, with some nice wrap around
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│2│2│X│5│ Moving to the third
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│2│2│3│5│ And back to the initial digit (the second)
└─┴─┴─┴─┘
Your algorithm will look very much like that. I would use a vector or an array to store digits, and a number like 10 (too big for a single digit) or -1 (not a positive, single digit) to indicate an unfilled spot. Notice also how you have to keep track of where you started. Each step will be the distance to the next digit plus some multiple of the number of digits in your number.
If you wish to convert your final result to an actual number, remember that you have a maximum number of digits. Use std::numeric_limits <int> ::digits10 to learn your maximum (replace int with the integer type you wish to use). Use your multiplication and addition facts to assemble the number, starting with the most significant digit (index 0 in your array or vector) and ending with the least significant digit.