Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何改进这个C代码来计算数字中的漏洞?_C# - Fatal编程技术网

C# 如何改进这个C代码来计算数字中的漏洞?

C# 如何改进这个C代码来计算数字中的漏洞?,c#,C#,我想用更优雅的C代码来解决这个难题 using System; public class Program { public static void Main() { int numero = 12345678; int check = CountHoles(numero); Console.WriteLine(check); } public static int CountHoles(int num){

我想用更优雅的C代码来解决这个难题

using System;

public class Program
{
    public static void Main()
    {
        int numero = 12345678;
        int check = CountHoles(numero);
        Console.WriteLine(check);   
    }

    public static int CountHoles(int num){
        int roles = 0;
        for (int i=0; i < num.ToString().Length; i++ ){
            string ver = "";                
            ver = num.ToString().Substring(i,1);                
            if (ver.Contains("0") || ver.Contains("4") || ver.Contains("6") || ver.Contains("9")){
                roles++;
            }               
            if (ver.Contains("8")){
                roles = roles+2;
            }
        }       
        return roles;       
    }
}
.Net小提琴:


谢谢:

您不需要将数字转换为字符串,您可以改进方法CountHoles,如下所示:

int holes = 0;
do{
        float tmp = (float) num / 10;    
        num /= 10;
        int ver = (tmp - num) * 10;

        if (ver == 0 || ver == 4 || ver == 6 || ver == 9)    holes++;             
        if (ver == 8)  holes += 2;

    }while (num != 0)       
    return roles; 

您不需要将数字转换为字符串,您可以改进方法CountHoles,如下所示:

int holes = 0;
do{
        float tmp = (float) num / 10;    
        num /= 10;
        int ver = (tmp - num) * 10;

        if (ver == 0 || ver == 4 || ver == 6 || ver == 9)    holes++;             
        if (ver == 8)  holes += 2;

    }while (num != 0)       
    return roles; 

我喜欢用字典映射代替if语句。可以使用LINQ从字符串中获取一个数字数组,然后使用另一个LINQ语句对结果求和,也可以将这两个语句组合成一个LINQ语句

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    private static Dictionary<int, int> _holeMap = new Dictionary<int, int>
    {
        { 0, 1 },
        { 1, 0 },
        { 2, 0 },
        { 3, 0 },
        { 4, 1 },
        { 5, 0 },
        { 6, 1 },
        { 7, 0 },
        { 8, 2 },
        { 9, 1 }                
    };

    public static void Main()
    {
        int numero = 12345678;
        int check = CountHoles(numero);
        Console.WriteLine(check);   
    }

    public static int CountHoles(int num){
        var digits = num
            .ToString()
            .Select(c => int.Parse(c.ToString()));

        return digits.Sum(d => _holeMap[d]);
    }
}

我喜欢用字典映射代替if语句。可以使用LINQ从字符串中获取一个数字数组,然后使用另一个LINQ语句对结果求和,也可以将这两个语句组合成一个LINQ语句

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    private static Dictionary<int, int> _holeMap = new Dictionary<int, int>
    {
        { 0, 1 },
        { 1, 0 },
        { 2, 0 },
        { 3, 0 },
        { 4, 1 },
        { 5, 0 },
        { 6, 1 },
        { 7, 0 },
        { 8, 2 },
        { 9, 1 }                
    };

    public static void Main()
    {
        int numero = 12345678;
        int check = CountHoles(numero);
        Console.WriteLine(check);   
    }

    public static int CountHoles(int num){
        var digits = num
            .ToString()
            .Select(c => int.Parse(c.ToString()));

        return digits.Sum(d => _holeMap[d]);
    }
}

我喜欢使用字典的想法。这是一个非常好的答案,但在这种情况下,我不能使用另一个依赖项,很抱歉,我之前忘记提到它,这是我在这里的第一个问题。@Emerson在这种情况下,您可以使用数组。var_holeMap=newint[]{1,0,0,0,1,0,1,0,2,1}。我喜欢使用字典的想法。这是一个非常好的答案,但在这种情况下,我不能使用另一个依赖项,很抱歉,我之前忘记提到它,这是我在这里的第一个问题。@Emerson在这种情况下,您可以使用数组。var_holeMap=newint[]{1,0,0,0,1,0,1,0,2,1}。我没有想到使用float和tmp。为什么?@Emerson,如果num=123,那么tmp将是12.3,num/10=12。。然后tmp-num*10=12.3-12*10=0.3*10=3。。我用这种方法来分隔数字,我没有想到使用float和tmp。为什么?@Emerson,如果num=123,那么tmp将是12.3,num/10=12。。然后tmp-num*10=12.3-12*10=0.3*10=3。。我用这种方法来分隔号码。我建议你改为访问该网站。因为它致力于改进现有的、有效的代码。谢谢:D我不知道这个问题。我投票结束这个问题,因为它要求代码审查。它可能更适合于。我建议您访问该网站。因为它致力于改进现有的、有效的代码。谢谢:D我不知道这个问题。我投票结束这个问题,因为它要求代码审查。它可能更适合你。