calculate less number of hops C# -


good morning :) i'm working on c# , want write code can calculate less number of hops point particular point, picture show

click here show picture

i have points 1 12, if want calculate less number of hops point 12 1 1 counterclockwise not 11 hops clockwise. example clarify question, if want calculate less number of hops point 11 4 5 counterclockwise not 6 hops clockwise. notice : number of points may odd number. hope understand question ..

try clockwise, anticlockwise , take minimum:

private static int hops(int a, int b) {   return math.min((12 + - b) % 12, (12 + b - a) % 12); } 

tests:

// 5 console.writeline(hops(11, 4)); // 1 console.writeline(hops(12, 1)); 

edit: matthew watson has mentioned in comments, may want know whether clockwise or anticlockwise:

private static int clockwisehops(int a, int b) {   return (12 + b - a) % 12; }  private static int anticlockwisehops(int a, int b) {   return (12 + - b) % 12; }  private static int hops(int a, int b) {   return math.min(clockwisehops(a, b), anticlockwisehops(a, b)); }  private static string solve(int a, int b) {   int hops = hops(a, b);    if (hops == clockwisehops(a, b))     return string.format("{0} -> {1} (clockwise) {2} hops", a, b, hops);   else     return string.format("{1} -> {0} (anticlockwise) {2} hops", a, b, hops); } 

tests:

 // 12 -> 1 (clockwise) 1 hops  console.writeline(solve(12, 1));  // 11 -> 4 (clockwise) 5 hops  console.writeline(solve(11, 4)); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -