Here a new problem I´ve solved right now!
Here first check the direction(beginning to the right), then check if start is < or > than combo[i], get the degrees, and continue with the other directions, until no more elements left. Then return the result as a double.
Here the problem statement:
Combination locks are convenient, because of their design. A combination lock consists of a small wheel to input the combination, and a latch to actually lock things together. The wheel contains consecutive numbers equally spaced around its edge, starting from zero and increasing clockwise. The outer casing of the lock has a small notch. To open the lock, you must rotate the wheel and align the numbers on the wheel with the notch in a specific order.
TopLock, a small company manufacturing combination locks, has recently decided to change its lock setup to give more security than the competitors. Many typical combination locks have a set of three numbers which you must input in order. TopLock locks will have a variable combination length which will be no less than three. In addition, they will be changing the number of numbers on the wheel, which will be between 10 and 100, inclusive.
To open a lock, you must follow the algorithm outlined below:
1. Set the current rotation direction to counterclockwise, and set N to the number of numbers in the combination.
2. Rotate the wheel N full turns in the current rotation direction, where a full turn is a 360 degree spin that leaves the notch pointing at the same number.
3. Continue to rotate the wheel in the current rotation direction until the notch is pointing at the next number in the combination, which successfully inputs that number. Note that by rotating the wheel counter-clockwise, the number that the notch points to will increase.
4. If there are no more numbers to input, open the lock, you’re finished! Otherwise, reverse the current rotation direction, decrease N by one, and go back to step 2.
It is guaranteed that no two adjacent numbers in combo will be the same, so as to avoid ambiguity in step 3. Also, the starting position of the lock, start, will not be the same as the first number in combo.
Given a int[] combo, indicating the combination of the lock, an int size, the number of numbers on the wheel, and an int start, indicating the number that the notch starts off pointing at, your method should return a double indicating the total number of degrees turned to open the lock.
For example, if combo = {10,20,30}, size = 40, and start = 6, you would first rotate the wheel three times counterclockwise and then 4 units to the 10, for a total of 3 * 360 + 4 * (360 / 40) = 1116 degrees, and then you would rotate twice clockwise and 30 units to the 20, and then once counterclockwise and 10 units to the 30. Your method would in this case return 1116 + 990 + 450 = 2556.
Here my code:
public class CombinationLock
{
public double degreesTurned(int[] combo, int size, int start)
{
double res = 0.0;
bool right = true;
for (int i = 0; i < combo.Length; i++)
{
int temp = 0;
if (right == true)
{
if (start < combo[i])
res += (((double)combo.Length - (double)i) * 360.0) + (((double)combo[i] - (double)start) * (360.0 / (double)size));
else
{
temp = (size - start) + combo[i];
res += (((double)combo.Length - (double)i) * 360.0) + ((double)temp * (360.0 / (double)size));
}
start = combo[i];
right = false;
}
else
{
if (start < combo[i])
{
temp = (start + size) - combo[i];
res += (((double)combo.Length - (double)i) * 360.0) + ((double)temp * (360.0 / (double)size));
}
else
{
temp = start - combo[i];
res += (((double)combo.Length - (double)i) * 360.0) + ((double)temp * (360.0 / (double)size));
}
start = combo[i];
right = true;
}
}
return res;
}
}
If you have another approach, please share with me in a comment!
Thank you very much

