该A - B问题【难度:1级】:
答案1:
public class Kata
{
public static int problem ( int A, int B)
{
return ( A - B) ^ 1 ;
}
}
答案2:
using System;
using System. Linq;
public class Kata
{
public static int problem ( int A, int B)
{
Random rand = new Random ( ) ;
int len = String. Join ( "" , ( A - B) . ToString ( ) ) . Length, n = rand. Next ( 1 , 8 ) , p= - 1 ;
return Convert. ToInt32 ( String. Join ( "" , ( A - B) . ToString ( ) . Select (
delegate ( char i)
{
++ p;
if ( p>= len- 1 )
{
if ( Char. IsDigit ( i) )
{
if ( n == Convert. ToInt64 ( i. ToString ( ) ) ) { if ( i < 9 ) { ++ i; } else { -- i; } return Convert. ToChar ( i. ToString ( ) ) ; }
else { return Convert. ToChar ( n. ToString ( ) ) ; }
}
else { return i; }
}
else { return i; }
} ) ) ) ;
}
}
答案3:
using static System. Math;
public class Kata
{
public static int problem ( int A, int B)
{
int correct = Abs ( A - B) ;
return correct % 10 == 9
? correct - 1
: correct + 1 ;
}
}
答案4:
public class Kata
{
public static int problem ( int A, int B)
{
if ( ( A- B) % 10 == 0 )
return ( A- B) + 1 ;
else return ( A- B) - 1 ;
}
}
答案5:
public class Kata
{
public static int problem ( int A, int B)
{
if ( ( A- B) % 10 == 0 )
{
return ( A- B+ 1 ) ;
}
else
{
return ( A- B- 1 ) ;
}
}
}
答案6:
public class Kata
{
public static int problem ( int A, int B)
{
if ( A == B) {
return 1 ;
}
var diff = System. Math. Abs ( A - B) ;
var sign = System. Math. Sign ( A - B) ;
if ( diff % 10 < 9 ) {
return sign* ( diff + 1 ) ;
} else {
return sign* ( diff - 1 ) ;
}
}
}
答案7:
using System;
public class Kata
{
public static int problem ( int A, int B)
{
var num = A - B;
if ( num % 10 <= 1 )
{
return num + 1 ;
}
return num - 1 ;
}
}
答案8:
public class Kata
{
public static int problem ( int A, int B) = > ( A- B) ^ 1 ;
}
答案9:
public class Kata
{
public static int problem ( int A, int B)
{
var r = A - B;
return ( r % 10 == 0 )
? r + 1
: r - 1 ;
}
}
答案10:
using System;
public class Kata
{
public static int problem ( int A, int B)
{
if ( ( A- B) % 10 == 9 )
return A- B - 1 ;
return A- B + 1 ;
}
}
答案11:
using System;
using System. Collections. Generic;
using System. Linq;
public class Kata
{
public static int problem ( int a, int b)
{
Console. WriteLine ( $"A: {a}, B: {b}" ) ;
var res = int . Parse ( string . Join ( "" , ( a - b) . ToString ( ) . ToList ( ) . Select ( c = > c) . Take ( ( a - b) . ToString ( ) . Length - 1 ) ) + ( 9 - char . GetNumericValue ( ( a - b) . ToString ( ) [ ( a - b) . ToString ( ) . Length - 1 ] ) ) . ToString ( ) ) ;
Console. WriteLine ( $"Actual: {a-b}" ) ;
Console. WriteLine ( $"Res: {res}" ) ;
return res;
}
}