C#练习题答案: 最大公约数位计数【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

最大公约数位计数【难度:1级】:

答案1:

using System;
using System.Linq;

public class BinaryGCD 
{
  public static int GcdBinary(int x, int y) 
  {
    return Convert.ToString(GCD(Math.Abs(x), Math.Abs(y)), 2).Count(e => e == '1');
  }
  
  private static int GCD(int a, int b)
  {
    return b == 0 ? a : GCD(b, a % b);
  }
}

答案2:

using System;

public class BinaryGCD 
{
  public static int GcdBinary(int x, int y) 
  {
    int a = Math.Abs(x);
    int b = Math.Abs(y);
    if (y==0)
    {
      string num = Convert.ToString(a,2).Replace("0","");
      return num.Length;
    }
        
    return GcdBinary(b, a % b);
  }
}

答案3:

using System;
using System.Linq;
using System.Numerics;
public class BinaryGCD 
{
  public static int GcdBinary(int x, int y) 
  {
    return Convert.ToString((int)BigInteger.GreatestCommonDivisor(x,y),2).Count(e=>e=='1');
  }
}

答案4:

public class BinaryGCD 
{
        static int gcd(int a, int b)
        {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        } 

        public static int GcdBinary(int x, int y)
        {
            int g = gcd(x, y);
            int cont = 0;
            while (g != 0)
            {
                if (g % 2 == 1 || g%2 == -1) cont++;
                g /= 2;
            }
            return cont;
        }

}

答案5:

using System;

public class BinaryGCD {
  public static int Gcd(int a, int b) {
    return (b == 0) ? Math.Abs(a) : Gcd(b, a % b);
  }
  public static int GcdBinary(int x, int y) {
    string gcdbin = Convert.ToString(Gcd(x, y), 2);
    int res = 0;
    foreach (char i in gcdbin) {
      if (i == '1') res++;
    }
    return res;
  }
}

答案6:

using System;
using System.Linq;

public class BinaryGCD 
{
  public static int GcdBinary(int x, int y) 
  {
    int gcd = GCD(Math.Abs(x), Math.Abs(y));
    
    return Convert.ToString(gcd, 2).Count(c => c == '1');
  }
  
  public static int GCD (int a, int b)
  {
    while (a != 0 && b != 0)
    {
      if (a > b)
        a %= b;
      else
        b %= a;
    }
    
    return a == 0 ? b : a;
  }
}

答案7:

using System;
using System.Linq;

public class BinaryGCD 
{
  public static int GcdBinary(int x, int y) 
  {
    int gcd = GCD(Math.Abs(x), Math.Abs(y));
    
    return Convert.ToString(gcd, 2).Count(c => c == '1');
  }
  
  private static int GCD(int a, int b)
  {
    return b == 0 ? a : GCD(b, a % b);
  }
}

答案8:

using System;
using System.Linq;

public class BinaryGCD 
{
  public static int GcdBinary(int x, int y) 
  {
    if (x == 0 && y == 0) return 0;
    return Convert.ToString(Math.Max(Gcd(Math.Abs(x), Math.Abs(y)), 1), 2).Count(c => c == '1');
  }
  private static int Gcd(int a, int b) => b == 0 ? a : Gcd(b, a % b);
}

答案9:

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

public class BinaryGCD
{
    public static int GcdBinary(int x, int y)
    {
        if (x == 0 && y == 0)
        {
            return 0;
        }

        if (x < 0)
        {
            x = -x;
        }

        if (y < 0)
        {
            y = -y;
        }

        int gcd = Gcd(x, y);
        string str = Convert.ToString(gcd, 2);
        return str.Count(c => c == '1');
    }

    private static int Gcd(int a, int b)
    {
        if (b == 0) return a;
        return Gcd(b, a % b);
    }
}

答案10:

using System;
using System.Linq;
   public class BinaryGCD
    {
        public static int GcdBinary(int x, int y)
        {
            string obraz_b = "";
            int b = Math.Max(Math.Abs(x), Math.Abs(y));
            int a = Math.Min(Math.Abs(x), Math.Abs(y));
            if(a == 0) 
            {
                     obraz_b = Convert.ToString(b, 2);
                     return obraz_b.Where(z => z == '1').Count();
            }
            while (b % a != 0 || Math.Min(x, y) % a != 0) a--;
            obraz_b = Convert.ToString(a, 2);
            return obraz_b.Where(z => z == '1').Count();
        }
    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值