【问题标题】:How can explain this array? [closed]如何解释这个数组? [关闭]
【发布时间】:2023-03-23 09:55:01
【问题描述】:
 int n = Convert.ToInt32(Console.ReadLine());
        int[] a = Array.ConvertAll(Console.ReadLine().Split(),Int32.Parse);
        int[] f = new int[101];
        
        for(int i=0;i<a.Length;i++)
            **f[a[i]]++;** //can not understand this line.
        
        int c = 0;
        for(int i=0;i<f.Length;i++)
            c += f[i]/2;
        
        Console.WriteLine(c);****

请帮忙解释一下我评论的那一行。

【问题讨论】:

    标签: c# arraylist


    【解决方案1】:

    这行f[a[i]]++可以分解如下:

    • a[i] 对应于在索引 i 中找到的当前值。例如,在循环的第一次运行中,值将是a[0] 等等。例如,如果你有a[0]=2,那么你的数组就是f[2]
    • f[2] 表示 f 数组第 3 位的值
    • 此值将增加 1

    我不太确定如何使用此值,因为您的 f 数组取决于 a 数组的值

    【讨论】:

    • 我想用这段代码找出 a[ ] 中有多少对元素
    猜你喜欢
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多