C#练习题答案: 该A - B问题【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

该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;
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值