0

首先很抱歉我的英语错误不是我的主要语言
我有问题,我有一个像下面这样的数组

string[] arr1 = new string[] { 
            "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
            "Pakistan:4,India:3,USA:2,Iran:1,UK:0", 
            "India:4,USA:3,Iran:2,UK:1,Pakistan:0" 
        };

现在我只想知道巴基斯坦有多少次带1,
有多少次带2、3、4,我需要知道所有印度、美国、伊朗、英国的情况

提前谢谢你们,你们是我最后的希望。

4

3 回答 3

1

我将使用 String.Split(char[]) 方法和 String.SubString(int, int) 方法检查数组中的每个“国家”并获取每个国家/地区的数字后缀。

尝试以下操作:

(以下代码现已编译和测试。)

使用简单的数据结构来简化保存操作结果的任务。

public struct Result {

    string Country { get; set; }
    int Number { get; set; }
    int Occurrences { get; set; }
}


// define what countries you are dealing with
string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", }

提供整体结果的方法:

public static Result[] IterateOverAllCountries () {

    // range of numbers forming the postfix of your country strings
    int numbersToLookFor = 4;        

    // provide an array that stores all the local results
    // numbersToLookFor + 1 to respect that numbers are starting with 0
    Result[] result = new Result[countries.Length * (numbersToLookFor + 1)];

    string currentCountry;

    int c = 0;

    // iterate over all countries
    for (int i = 0; i < countries.Length; i++) {

        currentCountry = countries[i];

        int j = 0;

        // do that for every number beginning with 0
        // (according to your question)

        int localResult;          

        while (j <= numbersToLookFor) {

            localResult = FindCountryPosition(currentCountry, j);

            // add another result to the array of all results
            result[c] = new Result() { Country = currentCountry, Number = j, Occurrences = localResult };

            j++;
            c++;
        }
    }

    return result;
}

提供本地结果的方法:

// iterate over the whole array and search the
    // occurrences of one particular country with one postfix number
    public static int FindCountryPosition (string country, int number) { 

        int result = 0;
        string[] subArray;

        for (int i = 0; i < arr1.Length; i++) {

            subArray = arr1[i].Split(',');

            string current;

            for (int j = 0; j < subArray.Length; j++) {

                current = subArray[j];
                if (
                    current.Equals(country + ":" + number) &&
                    current.Substring(current.Length - 1, 1).Equals(number + "")
                 ) 
                    result++;
            }
        }

        return result;
    }

以下应该使您能够运行算法

    // define what countries you are dealing with
    static string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", };

    static string[] arr1 = new string[] { 
        "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
        "Pakistan:4,India:3,USA:2,Iran:1,UK:0", 
        "India:4,USA:3,Iran:2,UK:1,Pakistan:0" 
    };

    static void Main (string[] args) {


        Result[] r = IterateOverAllCountries();
    }
于 2012-07-14T09:42:14.980 回答
1

此 linq 会将数组转换为 Dictionary>,其中外部字典包含国家名称,内部字典将包含出现次数(':' 之后的数字)和每次出现的计数。

string[] arr1 = new string[]
                            {
                                "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
                                "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
                                "India:4,USA:3,Iran:2,UK:1,Pakistan:0"
                            };

var count = arr1
    .SelectMany(s => s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(s => s.Split(':')[0], s => s.Split(':')[1])
    .ToDictionary(g => g.Key,
         g =>
         {
              var items = g.Distinct();
              var result = new Dictionary<String, int>();
              foreach (var item in items)
                  result[item] = g.Count(gitem => gitem == item);
              return result;
         });

// print the result
foreach(var country in count.Keys)
{
     foreach(var ocurrence in count[country].Keys)
     {
          Console.WriteLine("{0} : {1} = {2}", country, ocurrence, count[country][ocurrence]);
     }
}
于 2012-07-14T10:47:25.570 回答
0

您使用的数据结构不够丰富,无法为您提供该信息。因此,您需要解析您的字符串并创建一个新的数据结构以便能够提供 ( sring[][]):

        string[] arr1 = new string[] { 
        "Pakistan,India,USA,Iran,UK",
        "Pakistan,India,USA,Iran,UK", 
        "India,USA,Iran,UK,Pakistan" 
            };

        string[][] richerArray = arr1.Select(x=> x.Split('\'')).ToArray();
        var countPakistanIsFirst = richerArray.Select(x=>x[0] == "Pakistan").Count();

更新

你似乎改变了你的问题。答案适用于原始问题。

于 2012-07-14T09:40:32.087 回答